Click to See Complete Forum and Search --> : [RESOLVED] strange addslashes problem


Maximus9999
12-11-2007, 03:07 PM
in the database i have test\'s as a category

my sql statement prints out

SELECT * FROM products WHERE category='test\'s' and subcategory='Celebration Dresses' LIMIT 1 OFFSET 0;

and it finds nothing

but the statement below does find something

SELECT * FROM products WHERE category='test\\\'s' and subcategory='Celebration Dresses' LIMIT 1 OFFSET 0;

Does anybody know what the problem is?

NogDog
12-11-2007, 06:54 PM
Well, the first question to address is why you have a backslash as part of the value in the data. Most likely, it is due to double escaping as a result of having magic_quotes_gpc enabled in your PHP configuration, then "escaping the escape" by using addslashes() or mysql_real_escape_string() when adding that value into your insert/update SQL statement. See the mysql_real_escape_string page (http://www.php.net/mysql_real_escape_string[/url) for its "best practices" example to check for get_magic_quotes_gpc() and undo its effects before using mysql_real_escape_string() to prepare you strings for use is a query. (This is why magic_quotes_gpc will probably be eliminated in PHP 6, and why I'd recommend turning it off if you have control of the PHP environment.)

Anyway, the reason you needed all those backslashes is that, within your PHP string literal, you needed a backslash to escape the quote character, then you wanted a literal backslash before that to match the one in the database, but that backslash also needs to be escaped with . . . you guessed it . . . a backslash.

gomisute
12-12-2007, 02:16 AM
trying to use this to get rid of slashes

stripslashes($string);

Maximus9999
12-12-2007, 04:05 AM
Thanks, got it fixed

gomisute
12-12-2007, 09:47 AM
Thanks, got it fixed? So do you want to share your solution so others can learn/benefit, too?