PHP Create Table


PHP Create Table:– In the previous Tutorial we’ve learned how to create a database on MySQL server. Now it’s time to create some tables inside the database that will actually hold the data. A table organizes the information into rows and columns.

Creating Database Table

The SQL CREATE TABLE statement is used to create a table in database.Now we pass database name parameter also to create proper table into specific database. In below example we crate a table name ‘JohnData’ inside ’employee’ database.

Example // How to create a database table

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "employee";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//$sql="CREATE DATABASE employee";

$sql = "CREATE TABLE JohnData (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
    echo "Table 'JohnData' created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

Advertisements

Add Comment

📖 Read More