Search code examples
phpmysqlsqldatabasecreate-table

Syntax error on CREATE TABLE SQL query


What is wrong with the sql? I don't know what is wrong. It keeps giving me syntax errors.

$dbh->exec ("CREATE TABLE 'test'
             (col1 CHAR (64) PRIMARY KEY,
             col2 CHAR (64),
             col3 CHAR (64),
             col4 CHAR (64))") or die (print_r ($dbh->errorInfo (), true));

I'm using EasyPHP, just installed it. I'm trying to create a table using the code above. This is the error it returning. I tried correcting the syntax many times. I honestly think the syntax is correct. Is something to do with the configuration of my EasyPHP installation like maybe mysql configured incorrectly.

Array ( [0] => 42000 [1] => 1064 [2] => 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 1 )

Edit: Here is the code i'm using to connect:

function dbConnect ($dbname) {
    $user = "root";
    $pass = "";
    $db = new PDO ("mysql:host=127.0.0.1;dbname=$dbname", $user, $pass);
    if ($db) {
        //echo "Connected to database." . "</br>";
    }

    return $db;
}

Solution

  • Change your SQL to:

    CREATE TABLE test
                 (col1 CHAR (64) PRIMARY KEY,
                 col2 CHAR (64),
                 col3 CHAR (64),
                 col4 CHAR (64))
    

    Try with:

    $dbh->exec ("CREATE TABLE test
                 (col1 CHAR (64) PRIMARY KEY,
                 col2 CHAR (64),
                 col3 CHAR (64),
                 col4 CHAR (64))") or die (print_r ($dbh->errorInfo (), true));