CodeIgniter-查询绑定“;IN”;参数


CodeIgniter - query binding "IN" parameter

我的CodeIgniter中有一个下面的查询,我正试图通过参数绑定它。

$q = "SELECT * FROM my_table WHERE id IN (?)"
$ids = "1,2,3,4";
$this->db->query($q, array($ids));

以上操作不起作用,因为查询绑定将$ids视为字符串。如何参数化我的查询,但仍然能够进行"IN"操作?

编辑

对不起,我必须使用"原始SQL查询"。上面的查询只是一个更大更复杂的查询的一部分,我不能使用ActiveRecord。我也在使用Postgres。

将它放在数组中而不是字符串

$q = "SELECT * FROM my_table WHERE id IN (?)"
$ids =  array(1,2,3,4);
$this->db->query($q, $ids);

你可以在不绑定的情况下实现同样的效果

替代

$ids =  array(1,2,3,4);
$this->db->where_in('id',$ids);
$this->db->get('my_table');

在中使用where_in试试这个

$ids = array(1,2,3,4);
$this->db->select('*');
$this->db->from('my_Table');
$this->db->where_in('id',$ids);
$this->db->query($q, explode(',', $ids));

$this->db->where_in('id', explode(',', $ids))->get('my_table');

活动记录文档:http://ellislab.com/codeigniter/user-guide/database/active_record.html#select

像使用一样使用FIND_IN_SET

select * from table where FIND_IN_SET('$ids', id);

试试这个代码:

$ids = "('1','2','3','4')";
$q = "SELECT * FROM my_table WHERE id IN ".$ids;
$this->db->query($q);

你应该使用

$ids = array('1','2','3','4');
$this->db->where_in('id', $ids);

其中_in用于codignitor

点为

$ids = "1,2,3,4";
$a = array($ids);
//$a[0] = "1,2,3,4";
//not  $a[0]=1;$a[1]=2;$a[2]=3;$a[3]=4;

想要保持你的风格

 $ids = "1,2,3,4";
$a = explode(",",$ids);
$q = "SELECT * FROM my_table WHERE id IN ?"
$ids = "1,2,3,4";
$this->db->query($q, explode(",", $ids));