I currently have an array of values (business_id's)
lets say its
Array
(
[0] => 12
[1] => 14
[2] => 15
)
What is the best way to use each of these value to get the associated row information for each business_id from the database.
Can it be one in one query is my question. Obviously, you could do
$q = "select * from business where business_id = 12";
$rs = mysql_query($q);
解决方案$ids = array(1, 2, 3, 4);
$ids = join(', ', $ids);
$query = "SELECT * FROM business WHERE business_id IN ($ids)";
// $query => SELECT * FROM business WHERE business_id IN (1, 2, 3, 4)
The usual SQL injection warnings still apply, you may want to loop through the ids first to validate or escape them. Also, if you expect strings instead of numbers, use this:
$ids = array('a', 'b', 'c', 'd');
$ids = join("', '", $ids);
$query = "SELECT * FROM business WHERE business_id IN ('$ids')";
// $query => SELECT * FROM business WHERE business_id IN ('a', 'b', 'c', 'd')