Better mysqli table creation

<?php
$mysqli = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

if($mysqli->connect_errno) echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
echo $mysqli->host_info . "n";

$query = "CREATE TABLE hwt_tools(
id mediumint(8) NOT NULL AUTO_INCREMENT,
user_id mediumint(8),
category_id mediumint(8),
target_url varchar(255),
favicon_url varchar(255),
desc_text text,
UNIQUE KEY id (id)
);";

//Use this to safely create a table while testing
if(!$mysqli->query($query)) echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;

//Use this to drop existing table and start a new
//if(!$mysqli->query("DROP TABLE IF EXISTS hwt_tools") || !$mysqli->query($query)) echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;

?>

1 Comment

You must be logged in to post a comment.


  1. The CREATE TABLE statement is used to create a table in MySQL.
    We must add the CREATE TABLE statement to the mysqli_query() function to execute the command.

    http://w3webtutorial.blogspot.com/2013/11/php-mysqli-create-table.html