What are some best practices for creating a database for a website about adventure novels using PHP?
Issue: When creating a database for a website about adventure novels using PHP, it is important to follow best practices to ensure efficient data storage and retrieval. Code snippet:
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "adventure_novels";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create a table for adventure novels
$sql = "CREATE TABLE adventure_novels (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
author VARCHAR(50) NOT NULL,
genre VARCHAR(30) NOT NULL,
publication_year INT(4),
rating DECIMAL(2,1)
)";
if ($conn->query($sql) === TRUE) {
echo "Table adventure_novels created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Close the database connection
$conn->close();