Software Developer

Create MySQL Table in Existing Database With PHP

This page is in the Code Graveyard where I lay to rest snippets of code that I don't use anymore. Some have lost their usefulness due to the evolving nature of the web, while others have been replaced with better code. "Be Ye Warned!"
For the updated way to do this, see Create Table With PHP in Existing MySQL Database.

Create a MySQL database table using only PHP, for an existing database.


$dbhost = 'your.db.host';// you database host name goes here
$dbuser = 'databaseUsername';// you database username goes here
$dbpass = 'databasePassword';// you database password goes here
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE TABLE clients( '.// edit table name here. Mine is 'clients'
      /* do your fields */
     'P_Id int NOT NULL AUTO_INCREMENT, '.
     'field1 VARCHAR(40) NOT NULL, '.
     'field2  VARCHAR(7) NOT NULL, '.
     'field3  VARCHAR(20) NOT NULL, '.
     'field4  VARCHAR(2) NOT NULL, '.
     'field5  VARCHAR(4) NOT NULL, '.
     'primary key ( P_Id ))';
mysql_select_db('yourDatabaseName');// your existing database name here
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table clients created successfully\n";// edit table name here. Mine is 'clients'
mysql_close($conn);

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]