我有一个包含以下数据的数组:
array(3) { [0]=> array(1) { ["project_id"]=> string(2) "14" } [1]=> array(1) { ["project_id"]=> string(2) "21" } [2]=> array(1) { ["project_id"]=> string(2) "13" } }
我需要从数据库中回显返回所有行,其中项目ID等于数组中的ID.
使用下面的代码,我怎样才能只获得ID等于数据库中ID的记录???
function get_projects($id){
$data = '';
$this->db->where('id', $id); //HOW TO GET MULTIPLE IDS
$query = $this->db->get('projects');
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'user_id' => $row->user_id,
'project_name' => $row->project_name,
);
}
return $data;
}
解决方法:
使用where_in();
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')
有关更多信息,请参见the user guide.