Ffile2fix
Sign in Get started

How to fix MySQL "ERROR 1064: You have an error in your SQL syntax"

ERROR 1064 (42000): 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 'order (id, total) VALUES (1, 9.99)' at line 1

MySQL could not understand the SQL statement. The text after 'near' is where parsing stopped, and the real mistake is usually just before it. Common causes are reserved words used as names, missing quotes or commas, and syntax from another database or an older MySQL version.

Also appears as: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax · #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 · ERROR 1064 (42000) at line 88: You have an error in your SQL syntax · You have an error in your SQL syntax ... near 'TYPE=MyISAM' at line 6

Common causes

  • A reserved word such as order, group, key or rank used as a table or column name without backticks
  • Missing or mismatched quotes, commas or parentheses
  • Values inserted into SQL by string concatenation without escaping
  • Old syntax like TYPE=MyISAM instead of ENGINE=MyISAM
  • SQL written for PostgreSQL, SQL Server or SQLite (for example TOP, ILIKE, double-quoted identifiers)
  • A dump made on a newer server version imported into an older one

How to fix it

  1. Look just before the 'near' text. MySQL quotes the text starting at the point it got confused. Check the word or symbol right before it. 'near ''' at the end usually means something is unfinished, like a missing closing parenthesis.
  2. Backtick reserved words. Wrap table and column names that are reserved words in backticks, for example INSERT INTO `order` (`id`, `total`). Renaming the column is a cleaner long-term fix.
  3. Use prepared statements. Never build SQL by joining strings with user input. Use PDO or mysqli prepared statements with placeholders, which also prevents SQL injection.
  4. Replace outdated syntax. Change TYPE=MyISAM to ENGINE=InnoDB, and remove MySQL 5-era options your server no longer supports. Check the manual for your exact version with SELECT VERSION();.
  5. Convert other dialects. Replace SELECT TOP 10 with LIMIT 10, use backticks instead of double quotes for identifiers, and replace ILIKE with LIKE.
  6. Format long queries. Put each clause on its own line to make the broken part easy to spot, then run the query in the mysql client to get a precise line number.

Reserved word quoted and values passed safely (PHP PDO)

$sql = 'INSERT INTO `order` (`id`, `total`) VALUES (:id, :total)';
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => 1, 'total' => 9.99]);

How to stop it happening again

  • Avoid reserved words as table and column names
  • Always use prepared statements
  • Export and import dumps between matching MySQL versions
  • Format complex SQL so mistakes stand out

Frequently asked questions

Why does the query work in phpMyAdmin but not in my code?

Your code probably adds values that contain quotes, or builds the string differently. Print the final SQL or, better, switch to prepared statements.

What does 'near '' at line 1' mean?

MySQL reached the end of the statement while still expecting more. Look for a missing closing quote, parenthesis or value.

Is MariaDB syntax the same as MySQL?

Mostly, but newer features differ between them. Check the manual for the server you are actually running.