Skip to content

MySQL CREATE

To create database and table,

<?php

// mysql connect 
$con = mysql_connect('localhost', 'root', '');
if(!$con)
{
    die('Error in connecting: '.mysql_error());
}

/* the function mysql_connect_db is deprecated. It is preferable to
use mysql_query() to issue a sql CREATE DATABASE statement instead*/

// Create the database
$sql = "CREATE DATABASE test_db";
if(mysql_query($sql, $con))
{
    echo "Database Created successfully";
}
else
{
    echo "Error in creating:".mysql_error();
}

//Select the database
mysql_select_db("test_db",$con);

// Create the table
$create_table = "CREATE TABLE `table_name`(
    `table_id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(40) NOT NULL,
    `gender` VARCHAR(7) NOT NULL,
    `age` INT(2) NOT NULL
)";
if(mysql_query($create_table, $con))
{
    echo "Table created successfully";
}
else
{
    echo "Error in creating:".mysql_error();
}

?>

<< Back

MySql Insert

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: