Error 1064 You have an error in your SQL syntax: how to find the line

The 1064 is the database saying it did not understand the SQL statement: it is written in a way the database does not accept. The message always ends with a note like ... right syntax to use near 'FROM orders' at line 3. The golden rule is this: the fault is immediately before the quoted piece, not in it.

The causes that keep coming back

What you see The cause and the cure
A column called order, group, key or desc They are reserved SQL words. Put the name between backticks: `order`. Or rename the column.
Text containing an apostrophe (O’Brien) The apostrophe closes the text string halfway. The cure is not to “escape” by hand: it is to use prepared statements (parameters), which also close the door on SQL injection.
A stray comma before FROM or at the end of the list A comma after the last field. Count them.
An empty variable: WHERE id = with no value The code built the query with an empty variable. Write the query to the log before running it and see what was really sent.
It fails on import, at line 12874 The number is the line of the file. Open it in an editor and go to that line: very often the file is cut halfway (an interrupted upload).
Works on another server, not here Syntax from a different version. Here the engine is MariaDB, not Oracle’s MySQL. See MySQL or MariaDB: which one runs here.

Finding the line, step by step

1 Read the message to the end and copy the piece in quotes after near. Look for the fault just before it.
2 If it comes from an import, note the line number, open the file in an editor that shows line numbers and go there. If the file ends halfway through a statement, export it again or upload it again.
3 If it comes from code, write the complete query to the log before running it and paste it into the SQL tab of phpMyAdmin. It underlines where it fails. Where to see the log: where the PHP error log is.
4 Test in parts. Remove the suspect clause, run it, and put it back. Three attempts usually find it.
5 If a WordPress plugin raises the error, switch off the plugin named in the message and confirm the site comes back. See common WordPress errors.
Do not build queries by pasting in text from forms. Besides the 1064 that an apostrophe brings, that is how the door opens to SQL injection, one of the oldest and most exploited security flaws. Use parameters: a working example is in connecting to MySQL from PHP: mysqli and PDO.
A note on versions. Some statements exist in only one of the two engines. For example ADD COLUMN IF NOT EXISTS exists in MariaDB and not in Oracle’s MySQL. If a statement works on one machine and breaks on another, first check which engine and which version each one runs.

Cannot find the line? Send us the whole message and, for an import, the line number.

Open a support ticket

SEE ALSO

Connecting to MySQL from PHP: mysqli and PDO, with a working example

MySQL or MariaDB: which one runs here, and does it matter

Importing with phpMyAdmin, and the size limit that stops you

Where the PHP error log is

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from 5.940,00 Kz/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?