【问题标题】:where in condition codeigniter条件代码点火器在哪里
【发布时间】:2015-03-13 04:56:52
【问题描述】:

我正在使用 codeigniter 活动记录查询条件。

<?php
    foreach($std->result() as $stds_id) {
        $stid[] = $stds_id->std_id; 
        print_r($stid);
    }

实际上我想传递的是 where_in 是 Array ( [0] =&gt; 1 ) Array ( [0] =&gt; 2 [1] =&gt; 4 )

但它打印出来像

Array ( [0] => 1 ) Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 2 [2] => 4 )

我不知道如何通过它。

我的模特:

    $this->db->select('*');
    $this->db->where_in($field,$id);
    $getdata=$this->db->get($table);
    return $getdata;     

【问题讨论】:

  • 您的查询没问题,$stid 也没问题。只需在foreach循环外打印即可。只需在foreach循环后调用模型函数

标签: php mysql sql codeigniter


【解决方案1】:

您的代码看起来不错,只是您需要取出您在 foreach 中创建的数组。

$stid = array();
foreach($std->result() as $stds_id) {
        $stid[] = $stds_id->std_id;
}

$this->db->select('*');
$this->db->from('table');
$this->db->where_in('stid', $stid);
$this->db->get();
return $this->db->result();

【讨论】:

    【解决方案2】:

    将您的 foreach 循环替换为以下内容

    foreach ($std->result() as $stds_id) {
        $stid[] = $stds_id->std_id; 
    }
     print_r($stid);
    

    并使用此查询查询条件

    $this->db->where_in('stid', $stid);
    

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2012-02-09
      • 2011-06-04
      • 2016-01-28
      • 2012-06-30
      • 2012-02-21
      相关资源
      最近更新 更多