如何在 codeigniter 中使用 "where in" 方法?

How to use "where in" method in codeigniter?

大家好我的程序有问题我需要检查我的数据库中的多个数字但是当我测试它时只显示一个结果我的代码:

/*in mt View*/
$data = array(


          'name' => 'search_id',
          'id' => 'search_id',
          'placeholder' => 'numbers_test',
          'autofocus' =>"autofocus",
          'rows' => '20'
          );

echo form_textarea($data,set_value('search_id'));

/* in my model */

$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));

return $this->db->get();

我在等你解决这个问题

您必须return查询的结果。进行更改,

/* In my model */

$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));

$query = $this->db->get();
return $query->result_array();   // You've to return the result of the query


正如@Saqueib 在评论中所说,有疑问时请尝试调试

echo $this->db->last_query(); exit;

echo '<pre>'; print_r($query->result_array()); exit;

如果您从 $this->input->post('search_id') 中获取以逗号分隔的 ID 形式的输入,例如字符串 1,5,4,8 等,请像这样更新您的代码

/* in my model */

$this->db->select('*');
$this->db->from('personal_info');
// Explode string into array to make where_in clause work
$this->db->where_in('p_id', explode(',', $this->input->post('search_id')));

return $this->db->get();

as official docs 建议您需要在 IN 子句

中提供一系列选项