How to Create Tables in MySQL Database Using PHP?
1Answer
The CREATE TABLE statement is used to create a table in MySQL.
These Code create a table named "my_table", with five columns: "id", "firstname", "lastname", "email" and "mobile"
<html>
<head>
<title>Create table</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to create table
$sql = "CREATE TABLE my_table (
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
email VARCHAR(80),
mobile TIMESTAMP
)";
if (mysqli_query($conn, $sql)) {
echo "my_table created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
</body>
</html>
- answered 7 years ago
- Community wiki
Your Answer