Hi all,
My client's database has been escaping character fields using PHP's addslashes(), resulting in several records with fields containing values like "O\'Henry"
Querying for that data seems problematic as those fields aren't returned by LIKE "O'Henry%" or escaped, "O\\\'Henry%"
Hand-munging the escaping, I find that escaping the embedded backslash instead of the apostrophe, i.e. LIKE "O\\\\'Henry%" will fetch the desired records, as will escaping both the backslash and the apostrophe with LIKE "O\\\\\\\'Henry%"
So my problem, I guess, is how to have PHP do that munging on user-entered search strings, i.e. when the user entere "O'Henry" into a form field. If I use addslashes() or mysql_real_escape_string() on the user text, i.e.
name like '".trim(addslashes($_POST['name']))."%'
the resulting query will read "LIKE O\\\'Henry%" and fail to find the records. If I double-escape the search string, i.e.
name like '".trim(addslashes(addslashes($_POST['name'])))."%'
I'll get a query containing
name like 'Bob\\\\\\\'s Burgers%'
but that's really ugly, and there's no telling how many other queries such double-escaping would break.
Could someone please help me sort this mess out?
Thanks much,
Chuck
My client's database has been escaping character fields using PHP's addslashes(), resulting in several records with fields containing values like "O\'Henry"
Querying for that data seems problematic as those fields aren't returned by LIKE "O'Henry%" or escaped, "O\\\'Henry%"
Hand-munging the escaping, I find that escaping the embedded backslash instead of the apostrophe, i.e. LIKE "O\\\\'Henry%" will fetch the desired records, as will escaping both the backslash and the apostrophe with LIKE "O\\\\\\\'Henry%"
So my problem, I guess, is how to have PHP do that munging on user-entered search strings, i.e. when the user entere "O'Henry" into a form field. If I use addslashes() or mysql_real_escape_string() on the user text, i.e.
name like '".trim(addslashes($_POST['name']))."%'
the resulting query will read "LIKE O\\\'Henry%" and fail to find the records. If I double-escape the search string, i.e.
name like '".trim(addslashes(addslashes($_POST['name'])))."%'
I'll get a query containing
name like 'Bob\\\\\\\'s Burgers%'
but that's really ugly, and there's no telling how many other queries such double-escaping would break.
Could someone please help me sort this mess out?
Thanks much,
Chuck