当给定逗号分隔的字符串时,CodeIgniter 查询 in() 条件失败[重复]

问题描述 投票:0回答:3

我想使用 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 codeigniter select where-in
3个回答
1
投票
<?php
function addQuotes($string)
{
    return "'{$string}'";
}
$p = array('page1', 'page2', 'page3','page4', 'page5');
$p2 = array_map("addQuotes", $p);

print_r($p2);

查看实际操作

然后您就可以正常使用您的

implode()


0
投票

从外引号开始内爆

','

$pages = sprintf( "'%s'", implode( "','", $p ) );

0
投票

试试这个:

$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

© www.soinside.com 2019 - 2024. All rights reserved.