Search returns no rows in mysql query using LIKE and values having “\”

徘徊边缘 提交于 2019-12-13 05:14:05

问题


I have some problem regarding the search in mysql. Below is my query.

SELECT * FROM table WHERE name LIKE "%admin\'s%";

When i am executing this query it will return zero data.

actually i have "admin\'s" stored in db. this "\" is to prevent sql injection. i have used mysql_real_escape_string to prevent the sql injection.

but when i use three times addslashes to my variable it works.

So my below query is working.

SELECT * FROM table WHERE name LIKE "%admin\\\\\\\'s%";

My above query will return the data with name like admin's.

I am not getting where i am wrong.


回答1:


well for one you shouldnt have data like this in your DB admin\'s .. most likely you double escaped your string ( check if you don't have magic_quotes enabled on your server ).

If you only do

INSERT ... username = "admin\'s";

you will have in your db the username value admin's so my recomandation would be to go ahead and remove the slashes from your database and then your first query should work.



来源:https://stackoverflow.com/questions/2100622/search-returns-no-rows-in-mysql-query-using-like-and-values-having

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!