How can PHP be used to optimize the maintenance of multiple pages with different IDs for database entries?
When dealing with multiple pages that display database entries with different IDs, it can be challenging to maintain and update each page individually. One solution is to use PHP to dynamically generate the content based on the ID provided in the URL. By using PHP to query the database for the corresponding entry based on the ID in the URL, you can create a single template page that can display different content based on the ID parameter.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Get ID from URL
$id = $_GET['id'];
// Query database for entry with specific ID
$sql = "SELECT * FROM entries WHERE id = $id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data for specific entry
while($row = $result->fetch_assoc()) {
echo "<h1>" . $row['title'] . "</h1>";
echo "<p>" . $row['content'] . "</p>";
}
} else {
echo "Entry not found";
}
$conn->close();
?>
Related questions
-
file handling text parsing array storage
-
database entries class session recurring schedule
-
IDE Composer Xdebug