2014-11-05 64 views
0

我有下面的代碼我試圖run..Connection創建成功,但仍然mysqli_query不創建table.What我的思念......無法運行創建mysqli_query PHP SQL命令

這裏在script我執行...

error_reporting(E_ALL); 
    ini_set('display_errors', '1'); 
    $con=mysqli_connect("localhost","xxxxxx","xxxxx","xxx"); 
    if (mysqli_connect_errno()) { 
     echo mysqli_connect_error(); 
     exit(); 
    } else { 
     echo "Successful database connection"; 
    } 
    $tbl_users = "CREATE TABLE IF NOT EXISTS users (
        id INT(11) NOT NULL AUTO_INCREMENT, 
        firstname VARCHAR(255) NOT NULL, 
        lastname VARCHAR(255) NOT NULL, 
        email VARCHAR(255) NOT NULL, 
        password VARCHAR(255) NOT NULL, 
        gender ENUM('m','f') NULL, 
        state VARCHAR(255) NULL, 
        country VARCHAR(255) NULL, 
        userlevel ENUM('admin','user') NOT NULL DEFAULT 'user', 
        ip VARCHAR(255) NOT NULL, 
        signup DATETIME NOT NULL, 
        lastlogin DATETIME NOT NULL, 
        activated ENUM('0','1') NOT NULL DEFAULT '0', 
        PRIMARY KEY (email) 
       )"; 
    $query = mysqli_query($con, $tbl_users); 
    if ($query === TRUE) { 
     echo "<h3>user table created OK :) </h3>"; 
    } else { 
     echo "<h3>user table NOT created :(</h3>"; 
    } 
+0

是否有任何錯誤消息? – 2014-11-05 19:24:47

+0

代替'table not created',檢查實際的錯誤'echo mysqli_error($ con);' – 2014-11-05 19:25:18

+2

Id列AUTO_INCREMENT需要是主鍵,你有電子郵件作爲主鍵 – Mihai 2014-11-05 19:25:19

回答

0

您需要將主鍵更改爲AUTO_INCREMENT列,從電子郵件中刪除它,你可以把這個(電子郵件),以獨特的,如果它應該是

CREATE TABLE IF NOT EXISTS users (
        id INT(11) NOT NULL // >>> PRIMARY KEY //<<< AUTO_INCREMENT, 
        firstname VARCHAR(255) NOT NULL, 
0
[Err] 1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key 

PRIMARY KEY(電子郵件)是問題。將ID作爲主鍵。