Create mysql database and tables with php
Details
- Details
- Category: php
- Created on Tuesday, 22 May 2012 09:18
- Last Updated on Tuesday, 22 May 2012 09:22
- Published on Tuesday, 22 May 2012 09:18
- Written by Administrator
- Hits: 12564
Create database and table(s) in MySQL Database with php code:
<?php
//-----------------------
// www.developerpages.gr
//-----------------------
$con = mysql_connect("localhost","root","12345");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_test_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("my_test_db", $con);
$sql = "CREATE TABLE Persons
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";
// Execute query
if (mysql_query($sql,$con))
{
echo "<br>";
echo "Table created";
}
else
{
echo "<br>";
echo "Error creating Table: " . mysql_error();
}
mysql_close($con);
?>
And the output is :