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
)";
下一篇: 使用mysqli和php创建一个表