PHP | MySQL ( Creating Table )

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 :



  • My Personal Notes arrow_drop_up

    I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.