Remus Grigorescu
Remus Grigorescu

Reputation: 43

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

I want to create a table using the following script:

mysql_query("create table `".$mysql_table_prefix."query_log` (
    query varchar(255),
    time timestamp(14),
    elapsed float(2),
    results int, 
    key query_key(query)
)");

It gives me this error:

#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 'mysql_query("create table `".$mysql_table_prefix."query_log` ( query varchar(25' at line 1

EDIT: I used sql tab in phpMyAdmin

What can I do?

Upvotes: 3

Views: 12092

Answers (4)

Brad Koch
Brad Koch

Reputation: 20267

You're using PHP in a place that expects plain SQL (note that the OP stated "I used sql tab in phpMyAdmin"). Cut it down to:

create table `query_log` (
    query varchar(255),
    time timestamp,
    elapsed float(2),
    results int, 
    key query_key(query)
)

And @Aurelio is right, there's no length parameter to timestamp.

Upvotes: 1

Alfredo Castaneda Garcia
Alfredo Castaneda Garcia

Reputation: 1659

You could declare the whole text before passing it to the function:

    $params="create table `".$mysql_table_prefix."query_log` (
    query varchar(255),
    time timestamp(14),
    elapsed float(2),
    results int, 
    key query_key(query)
)"

Then just

mysql_query($params);

Upvotes: 0

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22152

The first error I see is that timestamp does not have a lenght. So this line:

time timestamp(14),

should be like this:

time timestamp,

Upvotes: 5

Marc B
Marc B

Reputation: 360672

Give your error message, you've screwed up the code somehow, such that the PHP mysql_query call is being treated as a string, then passed into mysql_query() directly. Possibly an unbalanced ' on a previous line is causing this.

... syntax to use near 'mysql_query("create table ...

A properly constructed query string would never have a literal PHP function call in it.

If you are passing a PHP script to mysql directly at the command line, e.g.

$ mysql < somescript.php

this will also not work. MySQL will not execute that script. It will attempt to load the raw text of code the literally.

Upvotes: 0

Related Questions