0

Well, me again!
Have an other problem with creating an other table. I want to create a Posts table with this code:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = 'CREATE TABLE Posts(
    ID INT(128) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Title CHAR(100),
    Author CHAR(100),
    Content CHAR(100),
    )';

    $conn->exec($sql);
    echo "Table Posts created successfully.<br>";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

But when i type the url of that file in the browser, this error appear on the page:

CREATE TABLE Posts( ID INT(128) UNSIGNED AUTO_INCREMENT PRIMARY KEY, Title CHAR(100), Author CHAR(100), Content CHAR(100), )
SQLSTATE[42000]: Syntax error or access violation: 1064 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 ')' at line 6

I didn't understand what is the syntax error for? The error says at line 6, but line 6 is empty!

2
Contributors
3
Replies
10
Views
2 Years
Discussion Span
Last Post by Niloofar24
0

OOPSS!! sorry i found the problem.
The problem was here Content CHAR(100), there was no need for ,.

1

check your coding for

$sql = 'CREATE TABLE Posts(
    ID INT(128) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Title CHAR(100),
    Author CHAR(100),
    Content CHAR(100),
    )';

should be

$sql = 'CREATE TABLE Posts(
    ID INT(128) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Title CHAR(100),
    Author CHAR(100),
    Content CHAR(100)
    )';

Don't add comma at the last field

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.