简体   繁体   English

MySQL 创建新表时出现语法错误

[英]MySQL syntax error while creating new table

I have been trying for hours to solve this issue, I have searched for the answers here and still can't figure it out what wrong with my code.我已经尝试了几个小时来解决这个问题,我在这里搜索了答案,但仍然无法弄清楚我的代码有什么问题。 I'm trying to create a new database and a table with PHP and MySQL:我正在尝试使用 PHP 和 MySQL 创建一个新数据库和一个表:

<?php
$hostname = 'localhost';
$username = 'root';
$password = '';

$connectDB = mysqli_connect($hostname, $username, $password);
if (!$connectDB) {
    die('Connection failed: ' . mysqli_connect_error());
}

$query = 'CREATE DATABASE user_records
    CREATE TABLE records_list (
    id INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(30) NOT NULL UNIQUE,
    userpass VARCHAR(30) NOT NULL)';

if (mysqli_query($connectDB, $query)) {
    echo '<div class="alertSuccess"><span>Query successful</span></div>';
} else {
    echo '<div class="alertError"><span>Query failed: <i>' . mysqli_error($connectDB) . '</i></span></div>';
}

?> ?>

And here's the error:这是错误:

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE records_list ( id INT(5) NOT NULL PRIMARY KEY AUTO_INCREME' at line 2

I'm using VSCode and XAMPP, latest MySQL 8.我正在使用 VSCode 和 XAMPP,最新的 MySQL 8。

Update: I have edited my code as @Phil and @Mark Walker suggested:更新:我按照@Phil 和@Mark Walker 的建议编辑了我的代码:

$queryCrDB = "CREATE DATABASE user_records;";
    
$queryCrTb = "USE user_records;
CREATE TABLE records_list (
id INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(30) NOT NULL UNIQUE,
userpass VARCHAR(30) NOT NULL);";

if (mysqli_query($connectDB, $queryCrDB)) {
    echo '<div class="alertSuccess"><span>Query successful</span></div>';
} else {
    echo '<div class="alertError"><span>Query failed: <i>' . mysqli_error($connectDB) . '</i></span></div>';
}
if (mysqli_query($connectDB, $queryCrTb)) {
    echo '<div class="alertSuccess"><span>Query successful</span></div>';
} else {
    echo '<div class="alertError"><span>Query failed: <i>' . mysqli_error($connectDB) . '</i></span></div>';

But it still gives me syntax error at CREATE TABLE line.但它仍然在 CREATE TABLE 行给我语法错误。

You need to run the create database before the create table.您需要在创建表之前运行创建数据库。 Query below should work, note the semi-colons closing individual commands and the "USE database" to switch into the newly created db.下面的查询应该可以工作,注意分号关闭各个命令和“使用数据库”以切换到新创建的数据库。

CREATE DATABASE user_records;
USE user_records;
CREATE TABLE records_list (
    id INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(30) NOT NULL UNIQUE,
    userpass VARCHAR(30) NOT NULL);

Based on mysql 8 doc , it should be:基于 mysql 8 doc ,它应该是:

id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY 

not不是

id INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT

PRIMARY KEY and AUTO_INCREMENT are reversed PRIMARY KEYAUTO_INCREMENT颠倒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM