View Single Post
  #1  
Old 05-04-2009, 05:46 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,566
Default PHP MySQL Create Database and Tables

A database holds one or multiple tables.
Create a Database

The CREATE DATABASE statement is used to create a database in MySQL.



Syntax

CREATE DATABASE database_name

To learn more about SQL, please visit our SQL tutorial.

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.
Example

The following example creates a database called "my_db":




$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
} mysql_close($con);
?>
Create a Table

The CREATE TABLE statement is used to create a table in MySQL.
Syntax

CREATE TABLE table_name(
column_name1 data_type,column_name2 data_type,column_name3 data_type,
....

)

To learn more about SQL, please visit our SQL tutorial.
We must add the CREATE TABLE statement to the mysql_query() function to execute the command.

Reply With Quote