MySQL Error 1064: SQL Syntax Error

MySQL error 1064 (SQL Syntax Error) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

MySQL Error Code 1064 MySQL Last verified 2026-08-19

Quick Answer

Read the 'near \'xxx\'' fragment: the error points at the token right after the problem. If that does not apply, fix the highlighted spot: usually a missing comma, unbalanced quote, or reserved word — the full checklist is below.

Error Code

MySQL Error Code: 1064
Official name: SQL Syntax Error
Service: MySQL

What does this error mean?

%s near '%s' at line %s

Common Causes

How to Fix

  1. Read the 'near \'xxx\'' fragment: the error points at the token right after the problem
  2. Fix the highlighted spot: usually a missing comma, unbalanced quote, or reserved word
  3. Quote reserved identifiers with backticks: `order`, `group`, `key`
  4. Test the query in the mysql CLI before wiring it into code, and use prepared statements for dynamic SQL
  5. For ORMs, log the final SQL (e.g. mysql2 sql property, SQLAlchemy echo) to see exactly what is sent

Code Examples

Minimal repro in the CLI sql
-- paste the failing statement and fix it iteratively:
SELECT id, order_id FROM orders;  -- `order` is reserved, use `orders`
SELECT id, `order` FROM `order`; -- or backtick-quote it

The mysql CLI shows the exact error position; fix it there, then port the corrected SQL back.

Reserved word fix sql
CREATE TABLE `group` (
  id INT PRIMARY KEY
);
SELECT * FROM `group`;

Backticks let you use reserved words as identifiers without renaming the column.

Framework-Specific Fixes

nodejs-mysql2

Log err.sql alongside the message; the snippet shows the exact statement that failed to parse.

catch (err) {
  console.error('SQL failed:', err.sql)
  // locate err.sqlPosition / the message fragment near the typo
}
python-sqlalchemy

Enable echo to print every emitted statement and catch syntax errors before they hit MySQL.

engine = create_engine(DATABASE_URL, echo=True)
# watch the generated SQL, especially for reserved words
php-pdo

Turn on exceptions and log the query string with the PDOException message.

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try { $pdo->query($sql); } catch (PDOException $e) {
  error_log($sql . ' :: ' . $e->getMessage());
}

You Might Also Like

Frequently Asked Questions

Why am I seeing MySQL error 1064?

Most often this happens when a typo or missing punctuation in hand-written SQL (commas, quotes, closing parentheses), or when using a reserved word (e.g. order, group, key) without backticks as a table or column name.

How do I fix MySQL error 1064?

Read the 'near \'xxx\'' fragment: the error points at the token right after the problem.

Which frameworks have documented fixes for error 1064?

This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.

Official Sources

This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.