How can dynamic loading of images from a database be implemented in PHP without creating individual pages for each entry?

To implement dynamic loading of images from a database in PHP without creating individual pages for each entry, you can use a single PHP script that retrieves the image data from the database based on a unique identifier (such as an ID) passed through the URL. This script can then output the image data along with the appropriate headers to display the image in the browser.

<?php
// 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);
}

// Get image ID from URL
$id = $_GET['id'];

// Retrieve image data from database
$sql = "SELECT image_data, image_type FROM images WHERE id = $id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();

    // Output image
    header("Content-type: " . $row['image_type']);
    echo $row['image_data'];
} else {
    echo "Image not found";
}

$conn->close();
?>