Open In App

PHP | MySQL ( Creating Table )

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

What is a table?
In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows.

Creating a MySQL Table Using MySQLi and PDO
We have already learned about creating databases in MySQL from PHP in this article. The steps to create table are similar to creating databases. The difference is instead of creating a new database we will connect to existing database and create a table in that database. To connect to an existing database we can pass an extra variable “database name” while connecting to MySQL.

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

In this article, a table named “employees”, with four columns: “id”, “firstname”, “lastname” and “email” will be created.

The data types that will be used are :

  1. VARCHAR:Holds a variable length string that can contain letters, numbers, and special characters. The maximum size is specified in parenthesis.
  2. INT :he INTEGER data type accepts numeric values with an implied scale of zero. It stores any integer value between -2147483648 to 2147483647.

The attributes that are used along with data types in this article are:

  1. NOT NULL: Each row must contain a value for that column, null values are not allowed.
  2. PRIMARY KEY: Used to uniquely identify the rows in a table. The column with PRIMARY KEY setting is often an ID number.

Creating tables in three different versions are described below:

  • Creating table using MySQLi Object-oriented Procedure
    Syntax :

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "newDB";
    
    // checking connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    // sql code to create table
    $sql = "CREATE TABLE employees(
            id INT(2)  PRIMARY KEY, 
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50)
            )";
    
    if ($conn->query($sql) === TRUE) {
        echo "Table employees created successfully";
    } else {
        echo "Error creating table: " . $conn->error;
    }
    
    $conn->close();
    ?>
    

    Output :

  • Creating table using MySQLi Procedural procedure
    Syntax :

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "newDB";
    
    // Checking connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    
    // sql code to create table
    $sql = "CREATE TABLE employees (
            id INT(2)  PRIMARY KEY, 
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50)
            )";
    
    if (mysqli_query($conn, $sql)) {
        echo "Table employees created successfully";
    } else {
        echo "Error creating table: " . mysqli_error($conn);
    }
    mysqli_close($conn);
    ?>
    

    Output :

  • Creating table using PDO procedure
    Syntax :

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "newDB";
    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", 
                                                    $username, $password);
        
        
        // setting the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        // sql code to create table
        $sql = "CREATE TABLE employees (
                id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                firstname VARCHAR(30) NOT NULL,
                lastname VARCHAR(30) NOT NULL,
                email VARCHAR(50)
                )";
    
        // using exec() because no results are returned
        $conn->exec($sql);
        echo "Table employees created successfully";
        }
    catch(PDOException $e)
        {
        echo $sql . "
    " . $e->getMessage(); } $conn = null; ?>

    Output :


  • Last Updated : 21 Mar, 2018
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads