想像一下我们有一个查询:
SELECT * FROM somewhere WHERE `id` IN(1,5,18,25) ORDER BY `name`;
和要获取的ID数组:$ids = array(1,25)
$stmt = $MysqLi->prepare('SELECT * FROM somewhere WHERE `id`=?;'); foreach ($ids as $id){ $stmt->bind_params('i',$id); $stmt->exec(); }
但现在我必须手动对结果进行排序.我有什么不错的选择吗?
你可以这样做:
$ids = array(1,25); // creates a string containing ?,?,? $clause = implode(',',array_fill(0,count($ids),'?')); $stmt = $MysqLi->prepare('SELECT * FROM somewhere WHERE `id` IN (' . $clause . ') ORDER BY `name`;'); call_user_func_array(array($stmt,'bind_param'),$ids); $stmt->execute(); // loop through results