PHP Create MySQL Table


A data table has a unique name and is composed of rows and columns.


Create MySQL Tables Using MySQLi and PDO

The CREATE TABLE statement is used to create a MySQL table.

Before creating the table, we need to use theuse myDBto select the database to operate on:

use myDB;

We will create a table named "MyGuests" with 5 columns: "id", "firstname", "lastname", "email" and "reg_date":

CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )

Notes on the table above:

The data type specifies what type of data the column can store. For a complete list of data types, refer to ourData Types Reference Manual。

After setting the data type, you can specify other optional attributes for each column:

  • NOT NULL - Each row must contain a value (cannot be empty), null values are not allowed.
  • DEFAULT value - Set a default value
  • UNSIGNED - Use an unsigned numeric type, 0 and positive numbers
  • AUTO INCREMENT - Set the MySQL field's value to automatically increase by 1 each time a new record is added
  • PRIMARY KEY - Sets a unique identifier for each record in the data table. Usually the column's PRIMARY KEY is set to the ID value and used together with AUTO_INCREMENT.

Each table should have a primary key (here, the "id" column), and the primary key must contain unique values.

The following examples show how to create a table in PHP:

Example (MySQLi - Object-Oriented)

<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; //Create connection $conn = new mysqli($servername, $username, $password, $dbname); //Check connection if ($conn->connect_error) { die("Connection failed:" . $conn->connect_error); } //Create table using sql $sql = "CREATE TABLE MyGuests ( 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 MyGuests created successfully"; } else { echo "Error creating table:" . $conn->error; } $conn->close(); ?>


Example (MySQLi - Procedural)

<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; //Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); //Check connection if (!$conn) { die("Connection failed:" . mysqli_connect_error()); } //Create table using sql $sql = "CREATE TABLE MyGuests ( 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 (mysqli_query($conn, $sql)) { echo "Table MyGuests created successfully"; } else { echo "Error creating table:" . mysqli_error($conn); } mysqli_close($conn); ?>


Example (PDO)

<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); //Set PDO error mode to throw exceptions $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Create table using sql $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; //Use exec(), no result is returned $conn->exec($sql); echo "Table MyGuests created successfully"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>

Other extensions