Creating a table in a MySQL database
This example shows how to create a table in a MySQL database using PHP . We are using our mydb database from a previous example and we will add a table called contact with two fields called fullname and email . Copy and paste the code below and access the page you should hopefully see the message Table 'mydb' was successfully created! .
Code :
<?php
//connect to your MySQL server using your servername , username
//and password
$conn = mysql_connect("localhost" , "username" , "password");
//if you cannot connect display an error message and exit
if ($conn == false)
{
echo mysql_errno().": ".mysql_error()."<BR>";
exit;
}
//create a table called contact , this will contain 2 fields .
//fullname to store a name and email to store an email address
$query = "create table contact" .
"(fullname varchar(255), email varchar(255))";
//store this in $result variable
$result = mysql_db_query ("mydb", $query);
//if successful display this message
if ($result)
echo "Table 'mydb' was successfully created!";
//if unsuuccessful display the error message
else
echo mysql_errno().": ".mysql_error()."<BR>";
mysql_close ();
?>