duanbi3385 2017-09-25 08:51 采纳率: 100%
浏览 30
已采纳

使用mysqli和php创建表

I am not sure what I am doing wrong here. The table won't be created in the database 'Users' and there is no error message at all. PhpMyAdmin is set to allow no password, just to be clear on that point.

here is my code:


    $dbConn = new mysqli("localhost", "root", "", "Users");

    if ($dbConn->connect_error)
    {
        echo "alert('Database connection: unsuccessful! " . $dbConn->connect_error . " ');";
        die("Connection failed: " . $dbConn->connect_error);
    }

    $mySql = "CREATE TABLE Users(
        ID string(255) NOT NULL,
        FirstName string(255) NOT NULL,
        Surname string(255) NOT NULL,
        DOB date(10) NOT NULL
    )";
    if ($dbConn->query($mySql) === TRUE)
    {
        echo "alert('Table creation: successful!');";
    }
    else
    {
        echo "alert('Table creation: unsuccessful! " . $dbConn->error . " ');";
    }

    $dbConn->close();

  • 写回答

1条回答 默认 最新

  • douyou8266 2017-09-25 09:02
    关注

    your query should be like this.

    $mySql = CREATE TABLE Users(
             ID VARCHAR(255) NOT NULL,
             FirstName VARCHAR(255) NOT NULL,
             Surname VARCHAR(255) NOT NULL,
             DOB date NOT NULL
        )";
    
    1. MySQL can't understand string. pass varchar instead of a string.
    2. you don't need to assign the length of date.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?