webneat
webneat

Reputation: 1

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 :)

Upvotes: 0

Views: 44

Answers (4)

CrackerJack9
CrackerJack9

Reputation: 3651

Need to use LIKE with the variable encased in braces:

SELECT * FROM ads WHERE country LIKE '%{$usercountry}%';

Upvotes: 0

Andrey
Andrey

Reputation: 1818

SELECT * FROM ads WHERE country like '%$usercoutry%'

Upvotes: 1

Vincent
Vincent

Reputation: 1035

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.

Upvotes: 0

John M
John M

Reputation: 14668

You could try:

SELECT * FROM ads WHERE ads.country = $usercountry

Upvotes: 0

Related Questions