Create Table Mysql PHP


Prev Tutorial Next Tutorial

Create Table Mysql PHP

Table is the collection of row and column, using below SQL query we can create own table in database.

Create Table in database

create table table_name (
id int(5) NOT NULL auto_increment,,
f_name varchar(20)
l_name varchar(20)
)

For create table in database using PHP webpage; first conncet to database then use below code.

connect-db.php

Connect to database

<?php

$server = 'localhost';
$user = 'user_name';
$pass = 'password';
$db = 'database_name';

// Connect to Database

$connection = mysql_connect($server, $user, $pass)
or die("Could not connect to server ... \n" . mysql_error ());

// select database
mysql_select_db($db)
or die("Could not connect to database ... \n" . mysql_error ());

?>

create-table.php

Create Table in database

<?php

include 'connect-db.php';

$sql = "id int(5) NOT NULL auto_increment, f_name varchar(20), l_name varchar(20)";
$result = mysql_query($conn, $sql);

if($result) 
{
 echo "Table user created successfully......";  
}
else 
{
echo "Could not create table: ". mysql_error($conn);  
}  
  
mysql_close($conn);

?>

Output

Table user created successfully......

Prev Tutorial Next Tutorial

Advertisements




Download Projects