我试图从一个数据库中获取数据,条件是id等于当前登录的租户(tenant_id),
$this->data['props'] = $this->property_m->get_by( array ('id' => $this->session->userdata['tanant_id'] ));
然后从字段中获取一些值并保存在数组中,
if(count($this->data['props']) > 0){
$my_array = array();
foreach ($props as $prop){
$my_array['id'] = $prop->tenant_id;
}
}
这里的第一个问题是$my_array只包含一个值。(我需要它是多重的)我该怎么做
第二个问题-我需要从另一个表中选择数据,该表将查找满足该数组中条件的数据,如
$this->data['reports'] = $this->report_m->get_by($my_array);
也就是说
SELECT * FROM reports WHERE ('id = 1'); // or 2 or 3 (value from prop->tenant)
但我需要它,
SELECT * FROM reports WHERE ('id = 1 OR id = 2 OR id = 3');
执行:
$my_array = array();
foreach ($props as $prop){
$my_array['id'] = $prop->tenant_id;
}
您只是在覆盖$my_array
的id
密钥。使用此:
$my_array = array();
foreach ($props as $prop){
$my_array[] = $prop->tenant_id;
}
// print_r($my_array);
使用where field in ()
条件:
SELECT * FROM reports WHERE id IN (1, 2, 3);
假设你有:
$my_array = array(1,2,3);
// query string:
$q_str = 'SELECT * FROM reports WHERE id IN (' . implode(',', $my_array) . ')';