Collin ter Steege
Collin ter Steege

Reputation: 523

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?

Upvotes: 0

Views: 424

Answers (2)

Ivien
Ivien

Reputation: 457

Check your table DDL sql statement, there is an extra comma (,) before the closing parentheses in your sql.

Upvotes: 1

Omagerio
Omagerio

Reputation: 505

Remove the last comma , before the ) .

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

Upvotes: 3

Related Questions