What is the best practice for loading images from a database in PHP?

When loading images from a database in PHP, it is best practice to store the image path in the database rather than the actual image data. This allows for better performance and scalability. To display the image on a webpage, you can query the database for the image path and then use HTML to display the image.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);

// Query database for image path
$sql = "SELECT image_path FROM images WHERE id = 1";
$result = $conn->query($sql);

// Display image on webpage
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    echo "<img src='" . $row['image_path'] . "' alt='Image'>";
} else {
    echo "Image not found";
}

// Close database connection
$conn->close();
?>