1

I need do searching from longer string. In sql in locality is 'London' and i need use LIKE and string of more cities how in example.. This doesnt work. Please help me, thanks

$result = mysqli_query($con, "SELECT * FROM automoto WHERE (title LIKE '%$keyword%') AND locality LIKE '%London%Paris%' ");
4
  • how to write string of cities? Commented Jul 16, 2014 at 21:32
  • Use OR: (title LIKE '%$keyword%') AND (locality LIKE '%London%' OR locality LIKE '%Paris%') Commented Jul 16, 2014 at 21:33
  • This is pretty standard practice here. You may find you need to loop your words in PHP to assemble the query string too. Commented Jul 16, 2014 at 21:35
  • Thanks, and any nicer solution? Isnt possible to rewrite %London%Paris% to working shape? Commented Jul 16, 2014 at 21:41

2 Answers 2

2

The most reliable way would be to use one OR per city:

(title LIKE '%$keyword%') AND (locality LIKE '%London%' OR locality LIKE '%Paris%')

if you do not want to build up all these "or"-conditions, you could use a regex to match:

(title LIKE '%$keyword%') AND locality REGEXP '(London|Paris)'
Sign up to request clarification or add additional context in comments.

1 Comment

Have read that REGEXP searches can take arbitrary time and memory space. If that is indeed true, in spite of it's elegance (& legibility), the former may be preferred.
0

Build up a string containing each city in an OR:

SELECT * FROM automoto WHERE title LIKE '%$keyword%' AND (locality LIKE '%London%' OR  locality LIKE '%Paris%')

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.