Create MySQL Table

PHP MySQL Create Table
  • In mysql database, databases name must be unique name.
  • after create a data table while you can specify other optional attributes
    • NOT NULL- In this attributes null values are not allowed
    • DEFAULT value- Set default value
    • UNSIGNED- limits the stored data to positive number and zero
    • AUTO INCREMENT- Increases the value of the field by 1 each time new record.
    • PRIMARY KEY- store unique id number used with Auto Increment
MySQLi Object-Oriented

If run program in your local server like XAMPP, WAMPP, etc. so your server credentials like...

$servername= "localhost";
$username = "root";
$password= ""; (blank)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "student";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to create table
$sql = "CREATE TABLE student_data(
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL ,
second_name VARCHAR(50) NOT NULL ,
email VARCHAR(50) NOT NULL ,
DOB DATE NOT NULL ,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

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

$conn->close();
?>

MySQLi Procedural

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "student";

// 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 student_data(
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL ,
second_name VARCHAR(50) NOT NULL ,
email VARCHAR(50) NOT NULL ,
DOB DATE NOT NULL ,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

if (mysqli_query($conn, $sql)) {
echo "Table student_data created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

PDO

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "student";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// sql to create table
$sql = "CREATE TABLE student_data(
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL ,
second_name VARCHAR(50) NOT NULL ,
email VARCHAR(50) NOT NULL ,
DOB DATE NOT NULL ,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

// use exec() because no results are returned
$conn->exec($sql);
echo "Table student_data created successfully";
} catch(PDOException $e) {
echo $sql . "
" . $e->getMessage();
}

$conn = null;
?>