How can PHP be used to interact with databases and display dynamic content on a website, such as a list of games with logos?
To interact with databases and display dynamic content on a website using PHP, you can use PHP's built-in MySQL functions to connect to the database, retrieve the necessary data, and then dynamically generate HTML content to display on the webpage. To display a list of games with logos, you would need to store the game information (such as name and logo URL) in a database table, retrieve this data using PHP, and then dynamically generate HTML elements to display the games and logos on the webpage.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "games_db";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve game data from the database
$sql = "SELECT * FROM games";
$result = $conn->query($sql);
// Display the list of games with logos
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div>";
echo "<img src='" . $row['logo_url'] . "' alt='" . $row['name'] . "'><br>";
echo $row['name'];
echo "</div>";
}
} else {
echo "No games found";
}
$conn->close();
?>
Related questions
-
SQL queries optimization PHP scripts
-
for loop array iteration
-
PHP system() exec()