MySQL Selection problem
I have two tables on database user and ads when user visit the ads page I want to show him only the ads related to his country in the ads table , there is field called contry where I insert related countries like that country="Country1;Coutry2;Country3;..." and every user have a field with his country name How to select ads from ads table where the user's coutry 开发者_高级运维is in the country field I guess something like that
SELECT * FROM ads WHERE $usercoutry IN ads.country
Thanks for All , I found the solution
SELECT * FROM ads WHERE coutry LIKE '%{$usercountry}%'
That Code solved the problem :)
SELECT * FROM ads WHERE country like '%$usercoutry%'
You could try:
SELECT * FROM ads WHERE ads.country = $usercountry
Are you trying to do something like the following?
SELECT * FROM ads WHERE ads.country like '%' || $usercountry || '%';
Nota: you should use a parameter (eg a prepared statement with '?' in the query), and set the concatenated string as its value, in order to avoid SQL injection - if it's a concern.
Need to use LIKE with the variable encased in braces:
SELECT * FROM ads WHERE country LIKE '%{$usercountry}%';
精彩评论