How to handle single quote and double quote for MySQL/PHP

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to handle single quote and double quote for MySQL/PHP

Post by Neo » Tue Jun 08, 2010 12:59 pm

I was dealing with a problem with passing single quotes in an SQL string which made it fail to insert due to misidentification by the MySQL engine. There are several methods to handle this in PHP.

Here are the escape sequences for following characters.

\ Backslash (ASCII 92). Represent this character by \\
' Single quote (ASCII 39). Represent this character by \'
" Double quote (ASCII 34). Represent this character by \"

In simple terms, you can place \' instead of single quote and \" instead of double quote.

The best and the easiest way to do this is by passing the SQL string to function mysql_real_escape_string.

Hope this will help!
Post Reply

Return to “PHP & MySQL”