0

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

2
  • Thanks for All , I found the solution SELECT * FROM ads WHERE coutry LIKE '%{$usercountry}%' That Code solved the problem :) Commented Aug 24, 2011 at 23:08
  • Can you mark the correct solution by checking the checkbox outline next to the answer? Commented Aug 25, 2011 at 5:22

4 Answers 4

1
SELECT * FROM ads WHERE country like '%$usercoutry%'
Sign up to request clarification or add additional context in comments.

Comments

0

You could try:

SELECT * FROM ads WHERE ads.country = $usercountry

Comments

0

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.

Comments

0

Need to use LIKE with the variable encased in braces:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.