我想使用 mysql IN() 函数检查一个值数组,但如何将它与 codeigniter 一起使用?
这就是我目前正在做的事情,显然行不通。
$p = array('page1', 'page2', 'page3','page4', 'page5');
$pages = implode(",", $p);
$sql = "select page from pages where domains_id = ? and page in(?) and is_deleted=0";
$fields = array($domains_id, $pages);
$this->db->query($sql, $fields)
the output of the pages variable comes out to page1,page2,page3,... but the in function needs them seperated like this:
'page1','page2','page3'....
<?php
function addQuotes($string)
{
return "'{$string}'";
}
$p = array('page1', 'page2', 'page3','page4', 'page5');
$p2 = array_map("addQuotes", $p);
print_r($p2);
然后您就可以正常使用您的
implode()
。
从外引号开始内爆
','
$pages = sprintf( "'%s'", implode( "','", $p ) );
试试这个:
$p = array('page1', 'page2', 'page3','page4', 'page5');
$this->db->select('page');
$this->db->from('pages');
$this->db->where('domains_id', $domains_id);
$this->db->where_in('page', $p);
//Note where_in function expects 2nd parameter as array
$this->db->where('is_deleted', '0');
$query = $this->db->get();
欲了解更多详情: http://ellislab.com/codeigniter/user-guide/database/active_record.html