How can PHP be used to extract file names from a database and display them alongside corresponding images in a gallery?
To extract file names from a database and display them alongside corresponding images in a gallery, you can first query the database to retrieve the file names and then use PHP to dynamically generate HTML code for displaying the images. You can use a loop to iterate through the results and create image tags with the file names as the source attribute. Finally, you can style the images using CSS to create a visually appealing gallery layout.
<?php
// Connect to database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Query to retrieve file names from database
$query = "SELECT file_name FROM images";
$result = mysqli_query($connection, $query);
// Display images in a gallery
echo '<div class="gallery">';
while ($row = mysqli_fetch_assoc($result)) {
echo '<img src="images/' . $row['file_name'] . '" alt="' . $row['file_name'] . '">';
}
echo '</div>';
// Close database connection
mysqli_close($connection);
?>
Keywords
Related Questions
- Why is it recommended to change the session ID after a successful login in PHP projects?
- Why is it recommended to avoid using the original mysql extension (mysql_ functions) in PHP due to deprecation and potential security risks?
- What are some common methods for compressing files in PHP, such as using GZip?