Entry
How can I use an apostrophe (') in a query string?
How do I escape single quotes in MySQL queries?
Jan 23rd, 2000 18:04
Nathan Wallace,
Strings in MySQL queries are delimited with single quotes. For example:
INSERT INTO my_strings VALUES ('test1');
If you want to insert a string that contains a single quote you need to
tell MySQL that it is part of the string, not the character denoting the
end of the string. To do this, we need to escape the single quote to be
contained in the string. You can do this in two ways. First by using
two single quotes together:
INSERT INTO my_strings VALUES ('I don''t know');
or by using the backslash character:
INSERT INTO my_strings VALUES ('I don\'t know');
You can read more about this in the manual:
http://www.mysql.com/Manual/manual.html#String_syntax
In PHP, the easiest way to escape problem characters is with the use of
the addslashes() function:
http://www.php.net/manual/function.addslashes.php3