无法使用mysqli_query php运行create sql命令

时间:2014-11-05 19:23:49

标签: php

我有以下代码我试图运行..连接已成功创建但仍然mysqli_query不会创建一个表。我错过了什么...

这是我正在执行的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>"; 
    }

2 个答案:

答案 0 :(得分:0)

您需要将主键更改为auto_increment列,将其从电子邮件中删除,您可以将此(电子邮件)设置为唯一(如果它应该是

CREATE TABLE IF NOT EXISTS users (
                  id INT(11) NOT NULL // >>> PRIMARY KEY //<<< AUTO_INCREMENT,
                  firstname VARCHAR(255) NOT NULL,

答案 1 :(得分:0)

[Err] 1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

PRIMARY KEY(电子邮件)是个问题。将id设为主键。