PHP | MySQL ( Creating Table ) - GeeksforGeeks

21 Mar, 2018
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 PDOWe 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 :
VARCHAR:Holds a variable length string that can contain letters, numbers, and special characters. The maximum size is specified in parenthesis.INT :he INTEGER data type accepts numeric values with an implied scale of zero. It stores any integer value between -2147483648 to 2147483647.
VARCHAR:Holds a variable length string that can contain letters, numbers, and special characters. The maximum size is specified in parenthesis.
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:
NOT NULL: Each row must contain a value for that column, null values are not allowed.PRIMARY KEY: Used to uniquely identify the rows in a table. The column with PRIMARY KEY setting is often an ID number.
NOT NULL: Each row must contain a value for that column, null values are not allowed.
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 ProcedureSyntax :<?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 :
<?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 procedureSyntax :<?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 :
<?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 procedureSyntax :<?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_upSave
<?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 :
mysql
PHP
SQL
Technical Scripter
Web technologies Questions
SQL
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
Comparing two dates in PHP
SQL | DDL, DQL, DML, DCL and TCL Commands
SQL | WITH clause
How to find Nth highest salary from a table
SQL | ALTER (RENAME)
SQL Trigger | Student Database