How to escape apostrophe (') in MySql?

The MySQL documentation says that it should be '. However, both scite and mysql shows that '' works. I saw that and it works. What should I do?

Asked By: user4951

Answer #1:

The MySQL documentation you cite actually says a little bit more than you mention. It also says,

A “'” inside a string quoted with “'” may be written as “''”.

(Also, you linked to the MySQL 5.0 version of Table 8.1. Special Character Escape Sequences, and the current version is 5.6 — but the current Table 8.1. Special Character Escape Sequences looks pretty similar.)

I think the Postgres note on the backslash_quote (string) parameter is informative:

This controls whether a quote mark can be represented by ' in a string literal. The preferred, SQL-standard way to represent a quote mark is by doubling it ('') but PostgreSQL has historically also accepted '. However, use of ' creates security risks...

That says to me that using a doubled single-quote character is a better overall and long-term choice than using a backslash to escape the single-quote.

Now if you also want to add choice of language, choice of SQL database and its non-standard quirks, and choice of query framework to the equation, then you might end up with a different choice. You don't give much information about your constraints.

Answered By: Jim DeLaHunt

Answer #2:

Standard SQL uses doubled-up quotes; MySQL has to accept that to be reasonably compliant.

'He said, "Don''t!"'
Answered By: Jonathan Leffler

Answer #3:

What I believe user2087510 meant was:

name = 'something'
name = name.replace("'", "\'")

I have also used this with success.

Answered By: user3169788

Answer #4:

There are three ways I am aware of. The first not being the prettiest and the second being the common way in most programming languages:

  1. Use another single quote: 'I mustn''t sin!'
  2. Use the escape character before the single quote': 'I mustn't sin!'
  3. Use double quotes to enclose string instead of single quotes: "I mustn't sin!"
Answered By: Robert Rocha

Answer #5:

Here's an example:

SELECT * FROM pubs WHERE name LIKE "%John's%"

Just use double quotes to enclose the single quote.

If you insist in using single quotes (and the need to escape the character):

SELECT * FROM pubs WHERE name LIKE '%John's%'
Answered By: Overkillica

Answer #6:

just write '' in place of ' i mean two times '

Answered By: MRRaja

Answer #7:

Replace the string

value = value.replace(/'/g, "\'");

where value is your string which is going to store in your Database.


NPM package for this, you can have look into it

Answered By: Ashutosh Jha

Answer #8:

I think if you have any data point with apostrophe you can add one apostrophe before the apostrophe

eg. 'This is John's place'

Here MYSQL assumes two sentence 'This is John' 's place'

You can put 'This is John''s place'. I think it should work that way.

Answered By: Priyanka Pandhi

Answer #9:

Possibly off-topic, but maybe you came here looking for a way to sanitise text input from an HTML form, so that when a user inputs the apostrophe character, it doesn't throw an error when you try to write the text to an SQL-based table in a DB. There are a couple of ways to do this, and you might want to read about SQL injection too. Here's an example of using prepared statements and bound parameters in PHP:

$input_str = "Here's a string with some apostrophes (')";
// sanitise it before writing to the DB (assumes PDO)
$sql = "INSERT INTO `table` (`note`) VALUES (:note)";
try {
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':note', $input_str, PDO::PARAM_STR);
} catch (PDOException $e) {
    return $dbh->errorInfo();
return "success";

In the special case where you may want to store your apostrophes using their HTML entity references, PHP has the htmlspecialchars() function which will convert them to '. As the comments indicate, this should not be used as a substitute for proper sanitisation, as per the example given.

Answered By: Grindlay

Answer #10:

In PHP I like using mysqli_real_escape_string() which escapes special characters in a string for use in an SQL statement.


Answered By: will
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

# More Articles