ixalmida;10926323 wrote:I personally don't like to have my quotes escaped (makes for difficult reading in MySQL admin), so I like to encode them as HTML instead:
// Store...
mysql_query("INSERT INTO MyTable (my_string) VALUES ('".htmlentities($string, ENT_QUOTES)."')";
// Retrieve...
$string = html_entity_decode($row['my_string']);
...but then I just like to be different. 🙂
The quotes should never end up being escaped in the actual data. When you use a function like mysql_real_escape_string(), it escapes the quotes for use in the SQL, but they do not make it into the actual data. If you are seeing escape characters in the actual data, then it is because you "double escaped" the input, and therefore escaped the escape character.
For example, if magic_quotes_gpc is enabled, then PHP prepends a back-slash to certain characters (including back-slashes, by the way). So if the input is foo 'quote' bar, then the $_POST value would be foo \'quote\' bar. If you then apply mysql_real_escape_string() to that value, you'd end up with foo \\'quote\\' bar, as both the quotes and back-slashes would now be escaped. Then when that double-escaped string is passed to a query, you'd end up with a literal back-slash character in the data (i.e., you'd be back to foo \'quote\' bar).
So long story short, if you turn off magic_quotes_gpc or undo its damage before applying your database-specific escaping mechanism, you should never have back-slashes inserted into your actual data. If you are seeing them, then you are double-escaping and probably need to turn off or negate magic_quotes_gpc (which is now deprecated and will not be available at all in PHP6).
Therefore, you should not need to convert quotes or other characters to HTML character entities. On top of that, doing so now changes the data and makes it HTML-specific, which means you have to account for those entities when sorting the data, searching it, or outputting it to something other than HTML. (And, of course, the data now take up more bytes - maybe not an issue in a TEXT column but it could be a serious problem in s VARCHAR column.)