我目前有一个值数组(business_id)
可以说
Array ( [0] => 12 [1] => 14 [2] => 15 )
使用这些值中的每一个从数据库中获取每个business_id的关联行信息的最佳方法是什么。
可以一一查询是我的问题。显然,你可以做
$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)
通常的SQL注入警告仍然适用,您可能需要先循环遍历id以进行验证或转义。另外,如果希望使用字符串而不是数字,请使用以下命令:
$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')