Create mysql database and tables with php



User Rating:  / 8
PoorBest 
Details

Create database and table(s) in MySQL Database with php code:

 

View source
<?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 :


You have no rights to post comments

   

Login  

   

     

© Developerpages