简体   繁体   中英

SQL or mariadb gives me this error when I try to create a table

I am trying to use PHP to create a table in MySQL. I already have made the database and PHP is connecting to MySQL just fine. Except it gave me this error and it won't create the table in the database.

Here is my code:

    <?php

         $servername = "localhost";
         $username = "root";
         $password = "";
         $dbname = "test";


$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "CREATE TABLE Test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
testname VARCHAR(30) NOT NULL,
)";

if ($conn->query($sql) === TRUE) {
    echo "Table created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}
$conn->close();
    ?>

The error I am getting:

Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 4

Could someone please help me?

Remove the last comma , before the ) .

CREATE TABLE Test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
testname VARCHAR(30) NOT NULL
)

检查您的表DDL sql语句,在sql中的右括号前有一个逗号(,)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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