Last Updated : 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
thisarticle. 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 :Creating tables in three different versions are described below:
<?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 = 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"; 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 . "Output :
" . $e->getMessage(); } $conn = null; ?>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4