Wednesday, April 3, 2019

mysql - 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();


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...