AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQL Create Table

PHP-MySQLi Tutorial » PHP MySQL Create Table

A database table for MySQL has its own unique name and is composed of columns and rows.

Create a MySQL Table Using MySQLi and PDO

We will create a table named "Students", with six columns as: "id", "first_name", "last-name", "email", "password" and "date":

CREATE TABLE Students(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(50),
password VARCHAR(50),
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

Meaning of attributes for each column:

Each table should have a primary key column (in this case: the "id" column). Its value must be unique for each record in the table.

 

Create a MySQL Table Using ( MySQLi Object-oriented ) - example

Open a Connection to MySQL

Before we can access data in MySQL database, we need to be able to connect to the server:

Example: (MySQLi Object-Oriented)

<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "myDB";



// Create connection
$con = new mysqli($server_name, $user_name, $password, $db_name);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}



// sql to create table
$sql = "CREATE TABLE Students (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";



if ($con->query($sql) === TRUE) {
echo "Table Students created successfully";
}
else
{
echo "Error creating table: " . $con->error;
}



$con->close();
?>

Note:
When you create a new database, you must only specify the first three arguments to the mysqli object (server-name, user-name and password).

 

MySQLi Procedural Example (MySQLi Procedural)

Example (MySQLi Procedural)

Example: (MySQLi Procedural)

<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "myDB";


// Create connection
$con = mysqli_connect($server_name, $user_name, $password, $db_name);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}


// sql to create table
$sql = "CREATE TABLE Students (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";


if (mysqli_query($con, $sql)) {
echo "Table Students created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);
}


mysqli_close($con);
?>


Tags: php mysql create table if not exist, foreign key, auto_increment, unique key, php mysql create table and insert data, primary key

 

Example (PDO) - example

Example (PDO)

Example: (PDO)

<?php
$serve_rname ="localhost";
$user_name ="username";
$password ="password";
$db_name ="myDB_PDO";

try {
$con = new PDO("mysql:host=$server_name;dbname=$db_name", $user_name, $password);
// set the PDO error mode to exception
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// sql to create table
$sql = "CREATE TABLE Students (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

// use exec() because no results are returned
$con->exec($sql);
echo "Table Student created successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}

$cnn = null;
?>

Note: In the PDO example above we have also specified a database (myDB_PDO).

 



php mysql create table if not exist, foreign key, auto_increment, unique key, php mysql create table and insert data, primary key
PHP MySQL Create Table - php mysqli

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.