How can you troubleshoot issues with creating a table in PHP using MySQL?
Issue: If you are experiencing issues creating a table in PHP using MySQL, you may want to check your SQL query syntax for any errors or typos. Make sure you have established a connection to the MySQL database before attempting to create a table. Additionally, ensure that the user you are using has the necessary permissions to create tables in the database.
<?php
// Establish a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to create a table
$sql = "CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
// Execute the SQL query
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Close the connection
$conn->close();
?>