tags:

views:

788

answers:

3

Im having a small problem with storage of special characters like quotes, double quotes and ampersands. I put every POST request through mysql_real_escape_string(), and when I add a string like "That '70s Show" it gets stored as "That '70s Show" in the mysql DB. When I echo it out, it works fine... but when I try to run a % $string % search for "That '70s Show", it will not find the record. I have magic_quotes disabled.

How can I get around this?

A: 

are you mysql_real_escape_string()'ing your %$string% search?

xkcd150
Yes, I am doing that.
Yegor
+1  A: 

Quoted from Mysql reference doc

There are several ways to include quote characters within a string:

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

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

  • Precede the quote character by an escape character (“\”).

  • A “'” inside a string quoted with “"” needs no special treatment and need not be doubled or escaped. In the same way, “"” inside a string quoted with “'” needs no special treatment.

The following SELECT statements demonstrate how quoting and escaping work:

mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello';
+-------+---------+-----------+--------+--------+
| hello | "hello" | ""hello"" | hel'lo | 'hello |
+-------+---------+-----------+--------+--------+

Refer : http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

Webrsk
+2  A: 

It looks like the problem is that you're not just running the incoming data that you store in the database through mysql_real_escape_string(), but also htmlentities() or a relative. Is that the case? If so, quit. :)

chaos
+1: Something more is definiteiy going on, as mysql_real_escape_string will change ' to '' or \' (I haven't checked which).
R. Bemrose
Another possibility is an encoding mismatch, but I'm holding out hope for the easy option.
chaos
Im not. Its just mysql_real_escape_string and thats it.
Yegor
Yegor
Yeah, htmlentities_decode().
chaos
Sorry, that should have been html_entity_decode(). Because God knows we can't have orthogonal function naming in PHP.
chaos
Yeah, html_entity_decode().
chaos