Escape Escape Characters - \\\\ for \\

This section provides a tutorial example on how to escape the MySQL escape character \ in PHP scripts.

MySQL server uses backslash "\" as the escape character to build escape sequences to provide special characters in string literals. Here are some examples:

\' - A literal single quote (') character.
\" - A literal double quote (") character.
\n - A newline (linefeed) character. 
\r - A carriage return character.
\t - A tab character.
\\ - A literal backslash (\) character.

If you are using MySQL client tool to run queries, using escape sequences are straightforward:

herong$ mysql 

mysql> USE MyBase;
Database changed

mysql> DROP TABLE MyNotes;
ERROR 1051 (42S02): Unknown table 'mybase.mynotes'

mysql> CREATE TABLE MyNotes (ID INTEGER, Note VARCHAR(1024));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO MyNotes 
    -> VALUES (1, 'We don\'t really know the speed of light');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO MyNotes 
    -> VALUES (2, 'We use backslash (\\) as the escape character');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO MyNotes 
    -> VALUES (3, '\\\\ is a literal backslash (\\) character');

mysql> SELECT * FROM MyNotes;
+------+----------------------------------------------+
| ID   | Note                                         |
+------+----------------------------------------------+
|    1 | We don't really know the speed of light      |
|    2 | We use backslash (\) as the escape character |
|    3 | \\ is a literal backslash (\) character      |
+------+----------------------------------------------+

mysql> SELECT * FROM MyNotes WHERE Note RLIKE '\\\\\\\\';
+------+-----------------------------------------+
| ID   | Note                                    |
+------+-----------------------------------------+
|    3 | \\ is a literal backslash (\) character |
+------+-----------------------------------------+

Note that MySQL also uses double quotes to protect the quoting character ' or ". For example:

mysql> INSERT INTO MyNotes VALUES (4, 'Tom is 5''9" tall');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO MyNotes VALUES (5, "Tom is 5'9"" tall");
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM MyNotes;
+------+----------------------------------------------+
| ID   | Note                                         |
+------+----------------------------------------------+
|    1 | We don't really know the speed of light      |
|    2 | We use backslash (\) as the escape character |
|    3 | \\ is a literal backslash (\) character      |
|    4 | Tom is 5'9" tall                             |
|    5 | Tom is 5'9" tall                             |
+------+----------------------------------------------+

When you use PHP scripts to run MySQL statements, we need to be careful about using the MySQL "\" escape character. This is because "\" is also the escape character for PHP string literals.

In PHP scripts, we need to escape the scape character '\' when coding MySQL statement as string literals. In other word, we need to use "\\" for each "\" character. For example:

$res = mysqli_query($con, "INSERT INTO MyNotes ".
  "VALUES (1, 'We don\\'t really know the speed of light')";

$res = mysqli_query($con, "INSERT INTO MyNotes ".
  "VALUES (2, 'We use backslash (\\\\) as the escape character')");

$res = mysqli_query($con, "INSERT INTO MyNotes ".
  "VALUES (3, '\\\\\\\\ is a literal backslash (\\\\) character')");

$res = mysqli_query($con, 
  "SELECT * FROM MyNotes WHERE Note RLIKE '\\\\\\\\\\\\\\\\'");

Here is a PHP script example showing you how to use the escape the escape character "\":

<?php
#  mysqli-escape-character.php
#- Copyright 2009-2026 (c) HerongYang.com. All Rights Reserved.

  $con = mysqli_connect("127.0.0.1", "herong", "TopSecret", 
    "MyBase");
  $res = mysqli_query($con, "DROP TABLE MyNotes");
  $res = mysqli_query($con, 
    "CREATE TABLE MyNotes (ID INTEGER, Note VARCHAR(1024))");

  # code \\' to send \'
  $res = mysqli_query($con, "INSERT INTO MyNotes ".
    "VALUES (1, 'We don\\'t really know the speed of light')");

  # code \\\\ to send \\
  $res = mysqli_query($con, "INSERT INTO MyNotes ".
    "VALUES (2, 'We use backslash (\\\\) as the escape character')");

  # code \\\\\\\\ to send \\\\
  $res = mysqli_query($con, "INSERT INTO MyNotes ".
    "VALUES (3, '\\\\\\\\ is a literal backslash (\\\\) character')");

  print("List all rows:\n");
  $res = mysqli_query($con, "SELECT * FROM MyNotes");
  while ($row = mysqli_fetch_array($res)) {
    print("   ".$row["ID"].", ".$row["Note"]."\n");
  }
  mysqli_free_result($res);

  # code \\\\\\\\\\\\\\\\ to send \\\\\\\\ 
  #   as regular expression pattern \\\\ to seach for \\
  print("Rows with \\\\:\n");
  $res = mysqli_query($con, 
    "SELECT * FROM MyNotes WHERE Note RLIKE '\\\\\\\\\\\\\\\\'");
  while ($row = mysqli_fetch_array($res)) {
    print("   ".$row["ID"].", ".$row["Note"]."\n");
  }
  mysqli_free_result($res);

  mysqli_close($con);
?>

If you run the script, you will get the following output:

herong$ php mysqli-escape-character.php 

List all rows:
   1, We don't really know the speed of light
   2, We use backslash (\) as the escape character
   3, \\ is a literal backslash (\) character

Rows with \\:
   3, \\ is a literal backslash (\) character

Table of Contents

 About This Book

 Introduction and Installation of PHP

 Managing PHP Environment and Modules on macOS

 Managing PHP Environment and Modules on CentOS

 Configuration and Modules

 cURL Module - Client for URL

 DOM Module - Parsing HTML Documents

 GD Module - Manipulating Images and Pictures

MySQLi Module - Accessing MySQL Server

 MySQLi Module and Configuration

 mysqli-hello.php - Test MySQLi Module

 mysqli_connect() - Open MySQL Connection

 mysqli_query() - Run SQL Queries

 mysqli_affected_rows() - Count Affected Rows

 mysqli_insert_id() - ID from AUTO_INCREMENT

 mysqli_fetch_array() - Fetch Row from Query Result

 mysqli_fetch_fields() - Fetch Field Information

Escape Escape Characters - \\\\ for \\

 Summary of Basic MySQLi Functions

 MySQLi Object-Oriented Programming

 OpenSSL Module - Cryptography and SSL/TLS Toolkit

 PCRE Module - Perl Compatible Regular Expressions

 SOAP Module - Creating and Calling Web Services

 SOAP Module - Server Functions and Examples

 Zip Module - Managing ZIP Archive Files

 References

 Index

 Full Version in PDF/EPUB