Php 其中,在使用codeigniter构建的查询中,有一个字符串应该是数组

Php 其中,在使用codeigniter构建的查询中,有一个字符串应该是数组,php,codeigniter,mysqli,codeigniter-3,where-in,Php,Codeigniter,Mysqli,Codeigniter 3,Where In,查询的where_in子句中的CodeIgniter出现问题 让我们看一下查询以描述问题: WHERE `table`.`table_row` IN ('7296 ,7314 {and so on}); 这是我在CodeIgniter(表达式引擎)中查询的结果 我的数组$entry\u id是一个字符串,我以前在代码中获得: $entry_ids = $ee->TMPL->fetch_param('e_id'); 要使用我的查询,请执行以下操作: WHERE `table`.`t

查询的
where_in
子句中的CodeIgniter出现问题

让我们看一下查询以描述问题:

WHERE `table`.`table_row` IN ('7296 ,7314 {and so on});
这是我在CodeIgniter(表达式引擎)中查询的结果

我的数组
$entry\u id
是一个字符串,我以前在代码中获得:

$entry_ids = $ee->TMPL->fetch_param('e_id');
要使用我的查询,请执行以下操作:

WHERE `table`.`table_row` IN ('7296' ,'7314' {and so on});
我已经尝试指定我的数组实际上是数组,而不是字符串:

$entry_ids[] = $ee->TMPL->fetch_param('e_id');

但实际上,什么都没有改变。

所以您将ID列表存储为字符串,您希望将其作为IN语句进行查询

首先将字符串拆分为数组:
如果您的ID由字符串分隔:

$id_array = explode(" ", $string_of_ids);
如果您的ID由逗号分隔:

$id_array = explode(",", $string_of_ids);
如果您的ID由逗号和单引号分隔:

$id_array = explode("','", $string_of_ids);
然后可以将$id\u数组传递到查询中:
如果id列的类型为int:

$this->db->where_in('table.table_row', $id_array);
如果id列的类型为字符串: 只需在ID中添加单引号:

$id_array = "'" . join("','", $id_array) . "'";
$this->db->where_in('table.table_row', $id_array);
同样,如果您的$string\u\u id包含ID周围的引号,您可以跳过一步,只需执行以下操作:

$id_array = explode(",", $string_of_ids);

这将保留您的报价,这样您就不必再次加入。

希望这将对您有所帮助:

使用
explode
将字符串放入数组中

  $string = '7296 ,7314'; // string of ids
  $entry_ids = explode(',', $string);
  //print_r($entry_ids);

  $this->db->where_in('table.table_row',$entry_ids); //use in where clause
工作演示:


更多信息

您遇到了什么错误?请分享您的全部代码('72967314')中的
table
table\u row
将失败,因为它不会返回任何内容。没有逗号来分隔每个值,或者列表(数字)的开头和结尾都有逗号。哪种类型的字符串只显示了一个示例,因此很容易纠正这个问题。正如我所说,它应该位于('7296','7314')中的
表行
或('72967314')中的
表行
中,请参见:
  $string = '7296 ,7314'; // string of ids
  $entry_ids = explode(',', $string);
  //print_r($entry_ids);

  $this->db->where_in('table.table_row',$entry_ids); //use in where clause