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

使用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();

图片转代码服务由CSDN问答提供 功能建议

我不确定我在这里做错了什么。 该表将不会在数据库“用户”中创建,并且根本没有错误消息。 PhpMyAdmin被设置为不允许密码,只是为了清楚这一点。

这是我的代码:

 
 
 $ dbConn =  new mysqli(“localhost”,“root”,“”,“Users”); 
 
 if($ dbConn-> connect_error)
 {
 echo“alert('数据库连接:不成功!”。$。  dbConn-> connect_error。“');”; 
 die(“连接失败:”。$ dbConn-> connect_error); 
} 
 
 $ mySql =“CREATE TABLE Users(
 ID string(  255)NOT NULL,
 FirstName字符串(255)NOT NULL,
姓氏字符串(255)NOT NULL,
 DOB日期(10)NOT NULL 
)“; 
 if($ dbConn-> query(  $ mySql)=== TRUE)
 {
 echo“alert('表创建:成功!');”; 
} 
 else 
 {
 echo“alert('表创建:不成功!'  。$ dbConn->错误。“');”; 
} 
 
 $ 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.
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题