mysqldude

Escaping text

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Unescaped backslashes are stripped (original)
original post
✓✓✓✓✓✓✓✓✓
Doubled backslashes are kept (original)
original post
✓✓✓✓✓✓✓✓✓
With sql_mode NO_BACKSLASH_ESCAPES the text is stored as typed
modern rewrite
✓✓✓✓✓✓✓✓✓
Unescaped backslashes are stripped (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Doubled backslashes are kept (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
With sql_mode NO_BACKSLASH_ESCAPES the text is stored as typed modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

The original article

First published 2011-11-01. The text below is the original; the table above shows how it behaves on current versions.

This blog posting is about the importance of escape the escape character in MySQL. The escape character in MySQL is the backslash () character.

If you are attempting to add the following text into a database “\frac{9}{5}”, the insert command will strip the backslashes out before inserting into the table. We will demonstrate this in this blog post.

Consider the following table

CREATE TABLE testtext(
  id INT AUTO_INCREMENT ,
  data TEXT,
  PRIMARY KEY ( id )
) ENGINE = InnoDB;

And then we insert into this table the following text “\frac{9}{5}”.

mysql> INSERT INTO testtext (data) VALUES ('\frac\{9\}\{5\}');
Query OK, 1 row affected (0.00 sec)

mysql> select * from testtext;
+----+------------+
| id | data       |
+----+------------+
|  1 | frac{9}{5} |
+----+------------+
1 row in set (0.00 sec)

We can see that the backslashes were stripped out during the insert. In order to insert data to maintain the backslashes we need to escape character these values too. So \ will need to become \. As follows:

mysql> INSERT INTO testtext (data) VALUES ('\\frac\\{9\\}\\{5\\}');
Query OK, 1 row affected (0.02 sec)

mysql> select * from testtext;
+----+-----------------+
| id | data            |
+----+-----------------+
|  1 | frac{9}{5}      |
|  2 | \frac\{9\}\{5\} |
+----+-----------------+
2 rows in set (0.01 sec)