<?php $db_host = "localhost"; $db_username = "root"; $db_pass = ""; $db_name = "onlinestores"; // Run the actual connection here $connect_dude=mysqli_connect("$db_host","$db_username","$db_pass","$db_name"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } ?> //creating table <?php include "connect_to_db.php"; $sql_command="CREATE TABLE admin( id NOT NULL auto_increment, username varchar(16) NOT NULL, password varchar(16) NOT NULL, last_login date NOT NULL, PRIMARY KEY(id), UNIQUE KEY username(username) )"; if(mysqli_query($connect_dude,"$sql_command")){ echo "ookie dookie"; }else { echo "it didn't work"; } ?>
Код не создает никакой таблицы в базе данных и показывает, что echo-оператор «он не работает».
У вашего SQL есть синтаксические ошибки.
id
column misses int
UNIQUE KEY username(username)
должно быть UNIQUE KEY(username)
Попробуй это:
$sql_command = "CREATE TABLE admin (id int NOT NULL auto_increment, username varchar(16) NOT NULL, password varchar(16) NOT NULL, last_login date NOT NULL, PRIMARY KEY(id), UNIQUE KEY(username))";
Чтобы уловить ошибки в вызовах mysql, используйте mysqli_error()
else { echo "it didn't work because: " . mysqli_error($connect_dude); }
Попробуйте это, убедитесь, что у вас есть собственные имена переменных, прежде чем нажимать код на производство,
Файл connect_to_db.php:
<?php $db_host = "localhost"; $db_username = "root"; $db_pass = ""; $db_name = "onlinestores"; // Run the actual connection here $mysqli=mysqli_connect($db_host, $db_username, $db_pass, $db_name); if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } ?>
Файл create_table.php:
<?php include "connect_to_db.php"; $sql_command="CREATE TABLE admin( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(16) NOT NULL UNIQUE KEY, pass VARCHAR(16) NOT NULL, last_login DATE NOT NULL )"; if($mysqli->query($sql_command) === TRUE) echo "ookie dookie"; else echo "it didn't work"; ?>
замените свой код:
$db_host = "localhost"; $db_username = "root"; $db_pass = ""; $db_name = "onlinestores"; // Run the actual connection here $connect_dude=mysqli_connect("$db_host","$db_username","$db_pass","$db_name"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } ?> //creating table <?php include "connect_to_db.php"; $sql_command="CREATE TABLE admin( id int NOT NULL auto_increment, username varchar(16) NOT NULL, password varchar(16) NOT NULL, last_login date NOT NULL, PRIMARY KEY(id), UNIQUE KEY username(username) )"; if(mysqli_query($connect_dude,"$sql_command")){ echo "ookie dookie"; }else { echo "it didn't work"; } ?>