How can you efficiently loop through and display multiple rows of data fetched from a MySQL database in PHP?
When fetching multiple rows of data from a MySQL database in PHP, you can efficiently loop through the data using a while loop. Inside the loop, you can fetch each row as an associative array and then display the desired data from each row.
<?php
// Establish a connection to the MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Fetch data from the database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
// Loop through the fetched data and display it
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row['name'] . "<br>";
echo "Age: " . $row['age'] . "<br>";
echo "Email: " . $row['email'] . "<br><br>";
}
// Close the database connection
mysqli_close($connection);
?>