Create database and table(s) in MySQL Database with php code:
//-----------------------
// 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 :
//-----------------------
// 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 :
Original Article : http://developerpages.gr/index.php/el/web-development-2/php/67-create-mysql-database-and-tables-with-php
No comments:
Post a Comment