Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > PHP Forum & Tutorial


PHP MySQL Create Database and Tables


Reply
Views: 3631  
Thread Tools Rate Thread
  #1  
Old 05-04-2009, 05:46 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 79,755
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
Reply

New topics in PHP Forum & Tutorial


Thread Tools
Rate This Thread
Rate This Thread:



Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)