MySQL error 1064 (SQL Syntax Error) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
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.
MySQL Error Code: 1064
Official name: SQL Syntax Error
System name: SQL syntax error
Service: MySQL
%s near '%s' at line %s
A Laravel app adds a column named rank to a leaderboard table. MySQL 8 reserves the word RANK for window functions, so the generated query SELECT id, rank FROM players fails with ERROR 1064 (42000): You have an error in your SQL syntax; ... near 'rank FROM players'. The quoted fragment in the message is where parsing broke down - the actual mistake is at or just before its first word. The same error fires for a trailing comma in an INSERT column list or MariaDB-only syntax run against MySQL.
Run these to confirm the cause before applying a fix — each command answers a specific question.
SELECT VERSION();
-- then check the keyword list for your version:
-- https://dev.mysql.com/doc/mysql-reserved-words/
Words like rank, groups, system, lead became reserved in 8.0 - code that worked on 5.7 can fail with 1064 on 8.0.
-- enable the general log briefly to capture the raw SQL
SET GLOBAL general_log = 'ON';
-- reproduce, then read the log file, then:
SET GLOBAL general_log = 'OFF';
The logged statement shows whether the ORM mangled quotes, commas, or placeholders before sending.
Parser error points at an identifier?
├─ it is a reserved word → backtick it: `rank` (or rename the column)
└─ typo / missing comma or quote → fix the SQL text
Error near a value / string?
├─ unescaped quote in user input → use bound parameters, never concatenation
└─ empty IN () clause → guard in code: if ids: query(...)
Worked on MariaDB or 5.7, fails on 8.0?
└─ engine-specific syntax (ALTER ... IF NOT EXISTS, new keywords) →
rewrite for MySQL 8 grammar; do not disable strict/ANSI modes
-- 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.
CREATE TABLE `group` (
id INT PRIMARY KEY
);
SELECT * FROM `group`;
Backticks let you use reserved words as identifiers without renaming the column.
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
}
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
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());
}
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.
Read the 'near \'xxx\'' fragment: the error points at the token right after the problem.
This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.