MySQL error 1064: how to read the message and find the broken part
MySQL error 1064 quotes the statement from the first token the parser could not accept, so look at that word and the one just before it for the cause.

On this page
Find the text after near in the message. That's where the parser gave up, and the mistake is either that first word or the thing directly before it. Here is the simplest case, a misspelt FROM:
SELECT * FORM scores;ERROR 1064 (42000) at line 1: 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 'FORM scores' at line 1The message has two useful parts: the quoted remainder of the statement, and a line number. The line is counted from the start of the statement that failed. In a script with several statements, the mysql program adds another number in front: the line of the file where that statement begins. So at line 4 in front and at line 3 at the end means the third line of a statement that starts on line 4, which is line 6 of the file. The quoted text is cut off after 80 characters, so a long remainder looks truncated even when nothing is. The server never gets as far as checking whether scores exists, which is why no table is needed for the examples below.
What the near text tells you
Text after near | Look for |
|---|---|
| Begins with a keyword you misspelt | The typo itself |
| Begins with your column or table name | A reserved word, or a missing comma before it |
) and nothing else | A trailing comma, or an empty list |
'' (empty) | The statement ended too early: empty variable, missing bracket, split procedure |
| Begins inside your data | An unescaped quote |
Begins with FROM or WHERE | A comma too many, or a function name used as a column |
Begins with IF NOT EXISTS, RETURNING, SEQUENCE | Syntax from MariaDB or another server |
Begins with IDENTIFIED BY or PASSWORD( | Account syntax that MySQL 8.0 removed |
Reserved words used as names
rank, groups and system were ordinary names in MySQL 5.7 and became reserved in 8.0, so an application can break on an upgrade without a line of it changing.
CREATE TABLE scores (id INT PRIMARY KEY, rank INT);ERROR 1064 (42000) at line 1: 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 'rank INT)' at line 1Backticks around the name fix it. Note that they're backticks, not single quotes. A name in single quotes is a string, and produces another 1064.
CREATE TABLE scores (id INT PRIMARY KEY, `rank` INT);
INSERT INTO scores (id, `rank`) VALUES (1, 3);
SELECT scores.rank FROM scores;+------+
| rank |
+------+
| 3 |
+------+The last line works without backticks because of a documented exception: a word after a period in a qualified name has to be an identifier. It's a handy trick in queries, but the keyword list with its (R) marks is the better place to look before naming a column.
Now the case where the near text points one word too late:
SELECT id, rank FROM scores;ERROR 1064 (42000) at line 1: 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 'FROM scores' at line 1Nothing is wrong with FROM scores. In this position RANK is the start of a window function, so the parser accepted the word and expected an opening bracket after it. What it found instead was FROM. Whenever the quoted text looks correct, read the word in front of it.
Aliases hide typos in the same way. SELECT 2 FORM x is reported near x, because FORM is a perfectly good alias for the column in front of it, and only the next word breaks the rule.
Commas and brackets
A trailing comma in a column list leaves the parser standing in front of a bracket:
CREATE TABLE invoices (
id INT NOT NULL AUTO_INCREMENT,
total DECIMAL(10,2) NOT NULL,
PRIMARY KEY (id),
);ERROR 1064 (42000) at line 1: 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 ')' at line 5Line 5 is the closing bracket, and the comma to delete is at the end of line 4. An opening bracket that never closes does the opposite. The parser reaches the end of the statement while still waiting, and quotes an empty string:
SELECT * FROM scores WHERE (id = 1;ERROR 1064 (42000) at line 1: 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 '' at line 1Spaces can matter too. COUNT (*) fails with near '*) FROM scores', because a handful of built-in functions must touch their bracket. The manual's page on function name parsing lists them and describes the IGNORE_SPACE SQL mode that relaxes the rule. Clause order is the other quiet one: LIMIT 1 ORDER BY id is rejected at ORDER BY, since LIMIT comes last.
Names with a hyphen need backticks as well. Without them my-table is read as a subtraction, and the message starts at -table.
Quotes: missing, doubled, or curly
An apostrophe in the data closes the string early. The rest of the name becomes stray SQL:
SELECT * FROM scores WHERE id = 'O'Brien';ERROR 1064 (42000) at line 1: 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 'Brien'' at line 1The near text starts inside your data, which is the signature of this bug. Typed by hand, you can write 'O''Brien', and the manual lists the other escapes under string literals. In application code, use a prepared statement, shown below.
Curly quotes, the kind word processors and chat programs insert, aren't quotes for MySQL at all. They count as letters that may appear in a name, so the error you get depends on the text between them:
<?php
$socket = getenv('LAB_SOCKET') ?: null;
$schema = getenv('LAB_DB') ?: 'shop';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('localhost', 'root', '', $schema, null, $socket);
$db->set_charset('utf8mb4');
foreach (["\u{2018}red\u{2019}", "\u{2018}red car\u{2019}"] as $value) {
try {
$db->query("SELECT * FROM scores WHERE id = $value");
} catch (mysqli_sql_exception $e) {
echo $e->getCode(), ' ', json_encode(strstr($e->getMessage(), 'near') ?: $e->getMessage()), "\n";
}
}1054 "Unknown column '\u2018red\u2019' in 'where clause'"
1064 "near 'car\u2019' at line 1"One word gives 1054, unknown column. Two words give 1064 at the second one. A non-breaking space copied from a web page behaves in a similar way: it glues SELECT to the next token, and the message quotes the statement from its first character. A byte order mark at the start of a file does it too. Retype the line rather than staring at it.
Building SQL in PHP
Most 1064 errors in PHP applications come from a variable that turned out empty. The statement is fine when the variable has a value:
<?php
$socket = getenv('LAB_SOCKET') ?: null;
$schema = getenv('LAB_DB') ?: 'shop';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('localhost', 'root', '', $schema, null, $socket);
$id = $_GET['id'] ?? ''; // nothing was sent
$sql = "SELECT * FROM scores WHERE id = $id";
try {
$db->query($sql);
} catch (mysqli_sql_exception $e) {
echo $e->getCode(), ': ', strstr($e->getMessage(), 'near'), "\n";
echo 'SQL was: [', $sql, "]\n";
}
$stmt = $db->prepare('SELECT * FROM scores WHERE id = ?');
$stmt->execute([$id]);
echo 'prepared: ', $stmt->get_result()->num_rows, " rows\n";1064: near '' at line 1
SQL was: [SELECT * FROM scores WHERE id = ]
prepared: 0 rowsPrinting the finished statement inside brackets is the debugging step that ends the search: you see the missing value at once. The prepared statement removes the problem, and closes the SQL injection hole that came with it. If you can't find where the message went, see where PHP errors are logged.
Three more variations on the same theme:
WHERE id IN ()from an empty array fails at). Test the array first and skip the query.- Placeholders stand for values only. A table or column name as
?is a 1064 at prepare time, so pick names from a fixed list in your code. LIMITwith a value bound as a string fails in PDO's emulated mode, near''2'', because the number arrives in quotes. Bind it withPDO::PARAM_INT.- Two statements in one
query()call fail at the start of the second one. Send them separately.
Procedures that fail at line 3
Pasting a stored procedure into the mysql program cuts it at its first semicolon:
CREATE PROCEDURE list_scores()
BEGIN
SELECT * FROM scores;
END;ERROR 1064 (42000) at line 1: 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 '' at line 3Empty near text again: the statement stopped at line 3, in the middle of the body. Change the delimiter around the definition, as the manual shows under defining stored programs:
DELIMITER //
CREATE PROCEDURE list_scores()
BEGIN
SELECT * FROM scores;
END//
DELIMITER ;
CALL list_scores();+----+------+
| id | rank |
+----+------+
| 1 | 3 |
+----+------+DELIMITER is a command of the mysql program and never reaches the server. Sent through mysqli it is itself a 1064, near DELIMITER //. From PHP, send the whole CREATE PROCEDURE ... END as one query without any delimiter line.
Right syntax, wrong server
The message says "the manual that corresponds to your MySQL server version" for a reason. These statements are all valid on some server, and all of them were rejected by MySQL 8.0.33:
| Statement | Why it fails there | Use instead |
|---|---|---|
ALTER TABLE t ADD COLUMN IF NOT EXISTS ... | MariaDB syntax | Check information_schema.columns first |
INSERT ... RETURNING id | MariaDB syntax | LAST_INSERT_ID() |
id UUID as a column type | MariaDB 10.7 and later | BINARY(16) with UUID_TO_BIN() |
CREATE SEQUENCE s | MariaDB syntax | An AUTO_INCREMENT column |
GRANT ... IDENTIFIED BY 'x' | Removed in MySQL 8.0 | CREATE USER, then GRANT |
SET PASSWORD ... = PASSWORD('x') | PASSWORD() removed in 8.0 | ALTER USER ... IDENTIFIED BY |
The removals are listed in the manual's what is new in MySQL 8.0, and the column type in MariaDB's UUID page. Two commands settle which server you have:
SELECT VERSION();
SHOW VARIABLES LIKE 'version_comment';A dump is the usual carrier of foreign syntax. When an import stops with 1064, the first number in the message is the line where the failing statement starts. Print a few lines around it, with sed -n '218,222p' dump.sql for line 220, and compare the statement with the table above. The mysqldump examples cover the flags that keep a dump portable.
Also check that the file really is SQL. An error at line 1 near <!DOCTYPE is a web page saved as dump.sql, and one near mysqldump: [Warning] means the program's own messages were redirected into the file along with the SQL. Redirect only standard output when you make the dump, so > and not 2>&1. phpMyAdmin reports the same server message in its import screen; if the import fails before any SQL runs, the size limits in upload_max_filesize are the likelier cause.
When nothing looks wrong
Cut the statement in half. Remove the ORDER BY, then the WHERE, then columns, until it runs, and put the pieces back one at a time. If even the first word is quoted as the error, suspect an invisible character in front of it, or the end of the previous statement: a missing semicolon makes two statements into one, and the parser complains at the start of the second.
Comments are worth one look too. -- starts a comment only when whitespace or a control character follows it, according to the manual. --note is two minus signs and a name, and it fails with the text starting at the dashes.
Same subject
MySQL EXPLAIN in practice: from a slow query to the right index
Put EXPLAIN in front of the query and read four columns: type, key, rows and Extra. They show whether an index is used, how well, and what is done after it.
nginx 502 Bad Gateway with PHP-FPM: read the error log line first
A 502 means nginx got no usable answer from PHP-FPM. One line in the nginx error log says why: no socket, no permission, a dead worker or a big header.
Where is php.ini? Finding the file PHP actually loads
Run php --ini for the command line, and call php_ini_loaded_file() through the browser for the website. They are often two different files, and both count.
PHPSolvedThe fix first, then the reason