Page 514 - Computer_Science_F5
P. 514
Program Example 8.2:
PHP program to generate dynamic content
<?php Chapter Eight: Databases and Database Management Systems
FOR ONLINE READING ONLY
// Connect to database
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “database”;
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: “ . $conn->connect_error);
}
// Fetch data from the database
$sql = “SELECT * FROM table”;
$result = $conn->query($sql);
// Display data on the webpage
echo “<ul>”;
while($row = $result->fetch_assoc()) {
echo “<li>” . $row[“column_name”] . “</li>”;
}
echo “</ul>”;
// Close connection
$conn->close();
?>
Step 2: Embed the PHP code within the HTML file using the `<?php ?>` tags are
shown in Program Example 8.3.
505
Student’s Book Form Five
Computer Science Form 5.indd 505 23/07/2024 12:35

