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
Service: MySQL
%s near '%s' at line %s
-- 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.