Jump to: navigation, search

PHP MySQLi Create Table

From w3cyberlearnings

Contents

PHP MySQLi Create Table

Create a table use $db->query() function.

Syntax Create Table


$create_table =
'CREATE TABLE IF NOT EXISTS student  
(
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(200) NOT NULL,
    age INT NOT NULL,
    PRIMARY KEY(id)
)';
$create_tbl = $db->query($create_table);

Example 1

<?php

define('HOST', 'localhost');
define('USER', 'user2000');
define('PASS', 'password2000');
define('DBNAME', 'w3cyberlearning');

$db = new mysqli(HOST, USER, PASS, DBNAME);

if ($db->connect_errno) {
	echo "Failed to connect to MySQL: (" 
    . $db->connect_errno . ") " . $db->connect_error;
}

$create_table =
'CREATE TABLE IF NOT EXISTS student  
(
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(200) NOT NULL,
    age INT NOT NULL,
    PRIMARY KEY(id)
)';

// Create student table
$create_tbl = $db->query($create_table);
if ($create_table) {
	echo "Table has created";
}
else {
        echo "error!!";  
}
$db->close();
?>

Output

Table has created 

<sidebar>

    • PHP MySQL mysqli|PHP MySQL mysqli
    • PHP MySQLi Connect|PHP MySQLi Connect
    • PHP MySQLi Create Table|PHP MySQLi Create Table
    • PHP MySQLi Insert Records Using Binding|PHP MySQLi Insert Records use Binding
    • PHP MySQLi Fetch Records as an associative array|PHP MySQLi Fetch Records as an associative array
    • PHP MySQLi Fetch All Records|PHP MySQLi Fetch All Records
    • PHP MySQLi Fetch Records And Bind to variables|PHP MySQLi Fetch Records And Bind to variables
    • PHP MySQLi Fetch as object|PHP MySQLi Fetch as object
    • PHP MySQLi Fetch Records Use binding|PHP MySQLi Fetch Records Use binding
    • PHP MySQLi Update Records and Return affected rows|PHP MySQLi Update Records and Return affected rows
    • PHP MySQLi Delete Records Use binding|PHP MySQLi Delete Records Use binding

</sidebar>

Navigation
Web
SQL
MISC
References