How can PHP be used to create a gallery with MySQL database integration?

To create a gallery with MySQL database integration using PHP, you can store image file paths in the database and retrieve them to display in the gallery. You can use PHP to connect to the MySQL database, fetch the image paths, and dynamically generate HTML to display the images in a grid layout.

<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "gallery";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch image paths from database
$sql = "SELECT image_path FROM images";
$result = $conn->query($sql);

// Display images in gallery
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<img src='" . $row["image_path"] . "' alt='Gallery Image'>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>