PHP + MySQL. Need coding help.?

  • Thread starter Thread starter ben
  • Start date Start date
B

ben

Guest
What is the php code for making a database and then using that database to create tables inside it.

Example: The database "example" is created and then I want to create a table called "table 1" inside database "example". What is the PHP coding for this? Is it possible to do this using variables?

Any help is appreciated!
 
<?php
//You use this to connect to the database:
$con = mysql_connect("localhost","your user name","your password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

//now you create the database

if (mysql_query("CREATE DATABASE example",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

//and now, the table:

mysql_select_db("example", $con);
$sql = "CREATE TABLE table1
(
// your fields here...
field1 varchar(15),
field2 varchar(15),
field3 int
)";

// Execute query

mysql_query($sql,$con);

//and close connection:

mysql_close($con);
?>
 
Back
Top