Skip to content
PHP Logo

How can PHP be used to display a list of different room types based on the data in a database?

To display a list of different room types based on data in a database, you can use PHP to query the database for the room types and then loop through the results to display them on the webpage. This can be achieved by connecting to the database, executing a query to retrieve the room types, fetching the results, and then displaying them in a list format on the webpage.

php
<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Query to retrieve room types
$stmt = $pdo->query('SELECT room_type FROM rooms');

// Fetch and display room types
echo '<ul>';
while ($row = $stmt->fetch()) {
    echo '<li>' . $row['room_type'] . '</li>';
}
echo '</ul>';
?>