How can using a database help in solving the problem of displaying a varying number of questions in a PHP quiz?
Using a database allows for storing an unlimited number of questions and dynamically retrieving them for display in a PHP quiz. By fetching questions from a database, the quiz can easily adapt to a varying number of questions without the need to hardcode them in the PHP script.
// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=quiz_db", "username", "password");
// Query to fetch questions
$stmt = $pdo->query("SELECT * FROM questions");
// Loop through fetched questions and display them
while ($row = $stmt->fetch()) {
echo "<h3>{$row['question']}</h3>";
// Display answer options, etc.
}