How to escape single quotes in MySQL

Ashok Gupta picture Ashok Gupta · May 20, 2009 · Viewed 249.9k times · Source

How do I insert a value in MySQL that consist of single or double quotes. i.e

This is Ashok's Pen.

The single quote will create problems. There might be other escape characters.

How do you insert the data properly?

Answer

Rob picture Rob · May 20, 2009

Put quite simply:

SELECT 'This is Ashok''s Pen.';

So inside the string, replace each single quote with two of them.

Or:

SELECT 'This is Ashok\'s Pen.'

Escape it =)