What is the correct syntax for retrieving multiple rows from a MySQL database in PHP?
When retrieving multiple rows from a MySQL database in PHP, you need to use a loop to fetch each row individually. You can achieve this by using a while loop along with the mysqli_fetch_assoc() function to fetch rows as associative arrays. This allows you to access the data in each row easily.
// Connect to the database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
// Query to retrieve multiple rows
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);
// Fetch and display each row
while ($row = mysqli_fetch_assoc($result)) {
echo $row['column1'] . ' - ' . $row['column2'] . '<br>';
}
// Close the connection
mysqli_close($connection);