How do you typically store paths and categories for individual images in a MySQL database when showcasing photos on a website?
When showcasing photos on a website, you can store the paths to the images and their corresponding categories in a MySQL database by creating a table with columns for the image path and category. This allows you to easily retrieve and display images based on their categories.
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydatabase";
$conn = new mysqli($servername, $username, $password, $dbname);
// Create table to store image paths and categories
$sql = "CREATE TABLE images (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
path VARCHAR(255) NOT NULL,
category VARCHAR(50) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table images created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Insert image paths and categories into the table
$sql = "INSERT INTO images (path, category) VALUES
('images/photo1.jpg', 'Nature'),
('images/photo2.jpg', 'Cityscape'),
('images/photo3.jpg', 'Portrait')";
if ($conn->query($sql) === TRUE) {
echo "Records inserted successfully";
} else {
echo "Error inserting records: " . $conn->error;
}
// Close database connection
$conn->close();