创建mysql表,如果它不存在

时间:2014-08-05 18:10:08

标签: php mysql

我不会使用php / mysql工作,但我需要我认为相对简单的任务:检查表是否存在并创建它,如果它没有。我甚至无法获得有用的错误消息,并且数据库中没有创建表。我的语法显然有问题。

<?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

?>

2 个答案:

答案 0 :(得分:7)

一些事情。

;中和)"

末尾有一个缺少的分号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
mysql_query

会导致/抛出一个解析错误,例如:

  

解析错误:语法错误,意外&#39;}&#39;在...

在我最初发布的代码的评论中显示的其他错误中。

另外,您在创建表格时没有使用mysqli_

这是一种ID方法,我在其中注释了您的原始代码。

旁注:您已在[{1}}中使用$query = "SELECT ID FROM " . $table;列,但您以小写字母创建了id的表格和列;两个字母都必须匹配。

<?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

?>

答案 1 :(得分:1)

    CREATE TABLE IF NOT EXISTS WEIGH-IN_DATA (
        id INT NOT NULL AUTO_INCREMENT,
        PRIMARY KEY(id),
        DATE    DATE NOT NULL,
        VALUE   SMALLINT(4) UNSIGNED NOT NULL )