Creating a table using mysqli and 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();


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
    )";
  • MySQL can't understand string. pass varchar instead of a string.
  • you don't need to assign the length of date.
  • 链接地址: http://www.djcxy.com/p/93694.html

    上一篇: 如何用示例的{$ variable}编写更新查询

    下一篇: 使用mysqli和php创建一个表