【问题标题】:Codeigniter Active Record Selecting Multiple Rows using Multiple Primary KeysCodeigniter Active Record 使用多个主键选择多行
【发布时间】:2011-12-28 04:57:01
【问题描述】:

我在动态返回多行时遇到问题。

我有一个活动记录查询需要从同一个表中返回多行。

//this will be a dynamic array of ids 
$array = array('01','02','03');

//i need to have other where conditionals as well
$cond['userlevel'] = 5;

//then add the array of ids to the conditionals array        
$cond['id'] = implode(',',$array);       

//then build the active record query
$q = $this->db->select($col->where($cond);

它似乎只返回 id 数组中的第一项。

【问题讨论】:

  • 尝试使用$q=$this->db->select('*')->from('table_name')->where($cond)->where_in('id',$array)->get();,也许这会起作用(记得把table_name改成你的);
  • 忘了说,这行:$cond['id'] = implode(',',$array);需要先去掉。
  • 你打赌谢谢。你应该回答这个问题,我会勾选。谢谢。

标签: php codeigniter activerecord


【解决方案1】:

试试类似的东西

"SELECT * FROM table_name WHERE userlevel IN(?,?,?,?)", array(0,1,2,3);

【讨论】:

    【解决方案2】:

    请试试这个,它会帮助你。

    $this->db->select('*');
    $this->db->from('table_name');
    $this->db->where_in('column_name',array(0,1,2,3));
    

    注意:- 确保 where_in 数组值不能为空,否则会出现 MySQL 错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多