How can PHP be used to generate random questions from a database for a web quiz?

To generate random questions from a database for a web quiz using PHP, you can first retrieve all the questions from the database, then use the `array_rand()` function to select a random question from the array. You can then display this random question on the quiz page.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "quizdb";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve all questions from the database
$sql = "SELECT * FROM questions";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Fetch all questions into an array
    $questions = $result->fetch_all(MYSQLI_ASSOC);

    // Select a random question
    $randomQuestion = $questions[array_rand($questions)];

    // Display the random question on the quiz page
    echo $randomQuestion['question'];
} else {
    echo "No questions found in the database.";
}

// Close the database connection
$conn->close();