Software Developer

Create Table With PHP in Existing MySQL Database

You have an existing MySQL database and you want to create a table using only PHP. You want to use mysqli_query, not mysql_query. Do it like this, but edit line 2 and line 11:


/* edit with your own database details */
$link = mysqli_connect("db.host", "your_db_username", "db_password", "db_name");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* choose your own table name */
$tablename = "umpaloompa";

$sql = "CREATE TABLE $tablename (
 `someid` INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 `field1` VARCHAR(25) NOT NULL,
 `field2` VARCHAR(18) NOT NULL,
 `field3` VARCHAR(45),
 `field4` TIMESTAMP
 ) CHARACTER SET utf8 COLLATE utf8_general_ci";

/* Create table */
if (mysqli_query($link, $sql) === TRUE) {
    printf("Table $tablename successfully created.\n");
}
else {
    printf("Could not create Table $tablename.\n");
}

mysqli_close($link);

See more:

By

We've One Response

  1. Pingback: Create MySQL Table in Existing Database With PHP

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]