Create mysql table if it doesn't exist
I don't work very much with php / mysql, but I need what I think would be a relatively simple task: check if a table exists and create it if it doesn't work. I can't even get a helpful error message and no table is created in the db. There is clearly something wrong with my syntax.
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
// 1. CONNECT TO THE DB SERVER, confirm connection
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "<p>Connected to MySQL</p>";
$mysql_connexn = mysql_connect("localhost", "root", ""); // redundant ?
// 2. CONNECT TO THE SPECIFIED DB, confirm connection
$db = "weighttracker";
mysql_select_db($db) or die(mysql_error());
echo "<p>Connected to Database '$db'</p>";
$db_connexn = mysql_select_db($db)or die(mysql_error("can\'t connect to $db"));
// 3. if table doesn't exist, create it
$table = "WEIGHIN_DATA";
$query = "SELECT ID FROM " . $table;
//$result = mysql_query($mysql_connexn, $query);
$result = mysql_query($query, $mysql_connexn);
if(empty($result)) {
echo "<p>" . $table . " table does not exist</p>";
$query = "CREATE TABLE IF NOT EXISTS WEIGHIN_DATA (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
DATE DATE NOT NULL,
VALUE SMALLINT(4) UNSIGNED NOT NULL
)"
}
else {
echo "<p>" . $table . "table exists</p>";
} // else
?>
source to share
A few things.
At the end and at the end )"
;
if(empty($result)) {
echo "<p>" . $table . " table does not exist</p>";
$query = "CREATE TABLE IF NOT EXISTS WEIGHIN_DATA (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
DATE DATE NOT NULL,
VALUE SMALLINT(4) UNSIGNED NOT NULL
)" // <--- right there
which would / would have caused a parsing error like:
Parse error: syntax error, unexpected '}' in ...
Among other errors, as shown in my comments from your originally posted code.
Also, you did not use mysql_query
in creating the table.
Here is the method mysqli_
where I commented out your source codes.
Sidenote: you use ID
for your column in $query = "SELECT ID FROM " . $table;
, and yet you create table and column as ID
lowercase; both characters must match.
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$DB_HOST = "xxx"; // put your own data
$DB_NAME = "xxx"; // put your own data
$DB_USER = "xxx"; // put your own data
$DB_PASS = "xxx"; // put your own data
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}
/*
// 1. CONNECT TO THE DB SERVER, confirm connection
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "<p>Connected to MySQL</p>";
$mysql_connexn = mysql_connect("localhost", "root", ""); // redundant ?
// 2. CONNECT TO THE SPECIFIED DB, confirm connection
$db = "weighttracker";
mysql_select_db($db) or die(mysql_error());
echo "<p>Connected to Database '$db'</p>";
$db_connexn = mysql_select_db($db)or die(mysql_error("can\'t connect to $db"));
// 3. if table doesn't exist, create it
$table = "WEIGHIN_DATA";
$query = "SELECT ID FROM " . $table; // that should be id and not ID
//$result = mysql_query($mysql_connexn, $query);
$result = mysql_query($query, $mysql_connexn);
*/
$table = "WEIGHIN_DATA";
$query = "SELECT ID FROM " . $table; // that should be id and not ID
//$result = mysql_query($mysql_connexn, $query); // your original code
// however connection comes last in mysql method, unlike mysqli
$result = mysqli_query($conn,$query);
if(empty($result)) {
echo "<p>" . $table . " table does not exist</p>";
$query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS WEIGHIN_DATA (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
DATE DATE NOT NULL,
VALUE SMALLINT(4) UNSIGNED NOT NULL
)");
}
else {
echo "<p>" . $table . "table exists</p>";
} // else
?>
source to share