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

PHP MySQLi Create Table


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP MySQLi Create Table

To create a table in MySQL database we use CREATE TABLE statement.

Syntax: SQL syntax to create a MySQL table

CREATE TABLE table_name (column_name column_type);

We will create a table named "Users", with six columns as: "id", "first_name", "last_name", "user_name", "email" and "reg_date":

CREATE TABLE Users (
id INT(7) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(42) NOT NULL,
last_name VARCHAR(42) NOT NULL,
user_name VARCHAR(42) NOT NULL,
email VARCHAR(65),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

Here are some explanations for each column:

 

Create table in DataBase MySQLi (object oriented)

The following examples create a table "Users" in database my_DB:

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = new mysqli($server_name, $user_name,
$password, $db_name);

// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}

// sql to create table

$sql = "CREATE TABLE Users (
id INT(7) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(42) NOT NULL,
last_name VARCHAR(42) NOT NULL,
user_name VARCHAR(42) NOT NULL,
email VARCHAR(65),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id))";

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

$link->close();
?>

 

Related subjects:
How to create database phpmyadmin? Insert multiple rows PHP select / view data Conect to MySQLi

How can I create a table for my database in MySQLi?
PHP MySQLi Create Table, temporary table, user, if not exists

 

Create table in DataBase MySQLi (Procedural)

The following examples create a table named "Users" in database my_DB using MySQLi (Procedural)

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = mysqli_connect($server_name, $user_name,
$password, $db_name);

// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to create table
$sql = "CREATE TABLE Users (
id INT(7) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(42) NOT NULL,
last_name VARCHAR(42) NOT NULL,
user_name VARCHAR(42) NOT NULL,
email VARCHAR(65),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id))";

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

mysqli_close($link);
?>

 

Create table Users in DataBase using (PDO) extension

The following example create a table named "Users" in database my_DB using PDO

Example (this is HTML - PHP editor, change text on this window)

<?php

$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";

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


// sql to create table
$sql = "CREATE TABLE Users (
id INT(7) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(42) NOT NULL,
last_name VARCHAR(42) NOT NULL,
user_name VARCHAR(42) NOT NULL,
email VARCHAR(65),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id))";

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

$link = null;
?>

 



How can I create a table for my database in MySQLi? PHP MySQLi Create Table, temporary table, user, if not exists
PHP MySQLi Create Table - php tutorial

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.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
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.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...