PHP Creating MySQL Tables
Before, I created a tutorial for
PHP MySQL Database. For the follow-up tutorial, we are going to learn on
How To Create MySQL Table In The Database. In the database, it will consist one or more tables. So, let’s start to create MySQL Table.
Note: All database table has a unique name.
Using MySQLi and PDO To Create MySQL Table.
We are going to use CREATE TABLE statement to make a table in MySQL.
-- --------------------------------------------------------
--
-- Table structure for table `tbl_registration`
--
CREATE TABLE `tbl_registration` (
`tbl_registration_id` INT(11) NOT NULL,
`first_name` VARCHAR(100) NOT NULL,
`middle_name` VARCHAR(100) NOT NULL,
`last_name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`contact_number` VARCHAR(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
So, we are going to create our table, name as
“tbl_registration” and contain with
“tbl_registration_id”, “first_name”, “middle_name”, “last_name”, “email”, “contact_number”.
Note: In each database table has an individual primary key. In the example table above, our primary key is “tbl_registration_id”. And it's value is unique.
Creating Database Table Using MySQLi (Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "add_query_pdo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection Error. Check your syntax." . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE `tbl_registration` (
`tbl_registration_id` int(11) NOT NULL,
`first_name` varchar(100) NOT NULL,
`middle_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`contact_number` varchar(100) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Your Table tbl_registration Successfully Created!!!";
} else {
echo "Error creating table. Check the Syntax." . $conn->error;
}
$conn->close();
?>
Creating Database Table Using MySQLi (Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "add_query_pdo";
// Create connection
// Check connection
if (!$conn) {
}
// sql to create table
$sql = "CREATE TABLE `tbl_registration` (
`tbl_registration_id` int(11) NOT NULL,
`first_name` varchar(100) NOT NULL,
`middle_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`contact_number` varchar(100) NOT NULL
)";
echo "Your Table tbl_registration Successfully Created!!!";
} else {
echo "Error creating table. Check the Syntax." . mysqli_error($conn);
}
?>
Creating Database Table Using PDO (PHP Data Objects)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "add_query_pdo";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to create table
$sql = "CREATE TABLE `tbl_registration` (
`tbl_registration_id` int(11) NOT NULL,
`first_name` varchar(100) NOT NULL,
`middle_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`contact_number` varchar(100) NOT NULL
)";
// use exec() because no results are returned
echo "Your Table tbl_registration Successfully Created!!!";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
Output:

So, this is it. You have one table in your database. And you can create more tables in your database. For my next tutorial, we are going to learn on
PHP Insert Data In MySQL.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at
[email protected]. Practice Coding. Thank you very much.