How can PHP be integrated with databases or external files to store quiz questions and answers dynamically?
To store quiz questions and answers dynamically, PHP can be integrated with databases such as MySQL or SQLite. By creating a database table to store the quiz questions and answers, PHP can then retrieve and display them on the quiz page. Alternatively, PHP can also read quiz questions and answers from external files such as JSON or XML files.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database_name");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Query to retrieve quiz questions and answers from database
$query = "SELECT question, answer FROM quiz_table";
$result = $mysqli->query($query);
// Display quiz questions and answers
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Question: " . $row["question"] . "<br>";
echo "Answer: " . $row["answer"] . "<br>";
}
} else {
echo "No quiz questions found.";
}
// Close database connection
$mysqli->close();
Keywords
Related Questions
- Is there a recommended method for handling temporary BIC number conversions in PHP, such as using Excel tables from the Bundesbank?
- What common mistakes or pitfalls should be avoided when writing SQL queries in PHP scripts?
- What is the correct way to output the results of array_unique() in PHP to avoid errors?