Reputation: 1352
I have an array()
in php. I want to search through mysql database simultaneously for all array items.
For example cars array as below:
$cars=array("Volvo","BMW","Toyota");
Besides Array size is variable. I need to search inside mysql database and see if specific row contains all array values or not?
something like this:
mysql_query("SELECT * FROM Person Where personCar contains $cars[0] AND $cars[1] AND $cars[2]);
personCar is a String. For example "Volvo, BMW, Toyota, Benz, ..."
Besides Array size is variable.
I would appreciate if you help.
I've thought of for loop and foreach loop but don't know the method in this case.
Upvotes: 0
Views: 57
Reputation: 349
$tmp = array();
foreach ($cars as $car) {
$tmp[] = "personCar LIKE '%".$car."%'";
}
$tmp = implode(" AND ", $tmp); //" OR " if you want to get a person which owns any of those cars
mysql_query("SELECT * FROM Person Where {$tmp};");
Upvotes: 2