Home > Software design >  Can't create a table if not exists [duplicate]
Can't create a table if not exists [duplicate]

Time:10-09

so this is my code to create e connect to a database and for create a table 'users' the problem is:
1-the code doesn't create the table (on phpmyadmin doesn't result any table) ;
2-I don't get any error message so i don't know what i'm doing wrong.
Any advice?

define('DBSERVER', 'localhost'); 
define('DBUSERNAME', 'root'); 
define('DBPASSWORD', '');
define('DBNAME', 'myDB');

$conn = mysqli_connect(DBSERVER, DBUSERNAME, DBPASSWORD, DBNAME);

if ($conn === false) {

die("Connection failed: " . mysqli_connect_error());
}

$conn->query('CREATE TABLE IF NOT EXIST users (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
password varchar(255) NOT NULL,
email varchar(100) NOT NULL,
permission varchar(20) DEFAULT Utente,
data TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY email (email)
ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1);');

CodePudding user response:

You have some errors in your create table sintax. EXISTS not EXIST. Utente should be in backticks. Closing ) should be before ENGINE.

AUTO_INCREMENT = 1 in this case it is not needed because it will start from 1.

Try below working code:

CREATE TABLE IF NOT EXISTS users (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
password varchar(255) NOT NULL,
email varchar(100) NOT NULL,
permission varchar(20) DEFAULT 'Utente',
data TIMESTAMP,
PRIMARY KEY `id`(`id`),
UNIQUE KEY `email`(`email`)
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1;
  • Related