2

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;
}
5
  • Can you show us what the stack trace looks like? Commented Mar 5, 2015 at 5:31
  • Can you also post the code you are using to connect to the database? Commented Mar 5, 2015 at 5:33
  • you got some advance, see the answers, don't forget check one as correct ;-) Commented Mar 16, 2015 at 17:48
  • @AdrianCidAlmaguer I still haven't gotten to work. Commented Mar 25, 2015 at 2:23
  • Has anyone figured out the problem yet? Commented Apr 12, 2015 at 3:54

2 Answers 2

2

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));
Sign up to request clarification or add additional context in comments.

7 Comments

It got rid of the error message. But when I check the database in phpadmin, the table is nonexistent.
@agentNil try to execute the query in phpmyadmin and see what happens
@agentNil do you got some advance?
What do you mean got some advance?
No, still don;t know why? Does this normally work? I'm not doing something i'm not suppose to am I? If that is the case, it should narrow down the possible problems to configuration and not the code itself.
|
2

Table names don't go inside quotes, change

CREATE TABLE 'test'
             ^    ^

To

CREATE TABLE `test`  // don't even need back ticks usually

3 Comments

That's what I had before.
If you had that before then there would not be any syntax error, with that change your query works just fine.
I've tried playing around with the quotes, backticsk, but doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.