PHP MySQL Create Table
Posted by tutor | Posted in PHP Tutorial | Posted on 18-12-2009-05-2008
0
The CREATE TABLE statement is used to create a table in a database of MySQL.
Syntax
-
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
….
)
We must add the CREATE TABLE statement to the mysql_query() function to execute the command.
<?php
$conn = mysql_connect(“localhost”,”tutor”,”tutor123″);
if (!$conn)
{
die(‘Could not connect: ‘ . mysql_error());
}
// Create database
if (mysql_query(“CREATE DATABASE my_db”,$conn))
{
echo “Database Creation:Successful”;
}
else
{
echo “Database Creation:Unsuccessful: ” . mysql_error();
}
// Create table
mysql_select_db(“my_db”, $conn); //Select database in which table is to be created
$sqlQuerry = “CREATE TABLE Country
(
Country varchar(15),
Capital varchar(15),
)”;
// Execute query
mysql_query($sqlQuerry,$conn);
mysql_close($con);
?>
Before creating a table you must select some database. The database is selected with the mysql_select_db() function.
Varchar() is a function which defines the data type for the variables containing alpha-numeric characters. You need to specify the maximum length of the field in varchar() e.g. varchar(15).
