0

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'....
4

3 に答える 3

1
<?php
function addQuotes($string)
{
    return "'{$string}'";
}
$p = array('page1', 'page2', 'page3','page4', 'page5');
$p2 = array_map("addQuotes", $p);

print_r($p2);

実際に見る

implode()その後、通常どおり使用できます。

于 2013-02-20T23:44:55.893 に答える
0

内破の外側の引用符から始めます','

$pages = sprintf( "'%s'", implode( "','", $p ) );
于 2013-02-20T23:45:08.470 に答える
0

Try this:

$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();

For more details: http://ellislab.com/codeigniter/user-guide/database/active_record.html

于 2013-02-21T07:31:31.547 に答える