Answer the question
In order to leave comments, you need to log in
How to check value in multiple columns at once?
Good day.
There is a text input where the user enters an address.
<input type="text" name="address" placeholder="Улица, Район, Округ">
$query = "SELECT * FROM `objects` WHERE `bid_type`='$bid_type' AND `object_type`='$object_type' AND `address` LIKE '%$address%' AND `okrug` LIKE '%$address%'";
Answer the question
In order to leave comments, you need to log in
If the bid_type and object_type fields do not apply to address and okrug , then so:
SELECT * FROM `objects`
WHERE `bid_type`='$bid_type'
AND `object_type`='$object_type'
AND (`address` LIKE '%$address%'
OR `okrug` LIKE '%$address%')
All beginners without exception think that their query is applied to the entire database at once.
Well, they want to find lines where both the address and the county are equal to some value.
But if you think about it, then the query compares each row individually with the condition.
And AND will look for a string in which both the address and the county are equal to this value.
Whereas they want to return every row in which the address OR the county was equal to some value.
That is, instead of AND when searching "in several columns" you need to use OR.
Of course, this does not apply to other conditions used in the request. They remain as they are, and the search "in several fields" is simply taken in brackets.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question