【问题标题】:how to use array in where clause in Codeigniter如何在 Codeigniter 的 where 子句中使用数组
【发布时间】:2016-02-09 19:18:35
【问题描述】:

我正在使用 codeigniter,尝试在 where 子句中使用数组并仅返回与数组匹配的 id。

表格提交:

$arrTID = $this->input->post('Present'); print_r($arrTID);

输出:

Array(
 [0]=>FMM003
 [1]=>30089
 [2]=>30097
)

在foreach中使用时:

foreach($arrTID as $key=>$ID){               
 print_r($ID);
}

输出:FMM0033008930097

查询:

$query = $this->db->query("select TraineeID from tbl_attendance_processed where TraineeID IN $ID and attnDate='$atnDate'");
$res=$query->result_array();

我实际上需要该查询返回与该查询匹配的数组中的那些 ID。这个怎么弄?

【问题讨论】:

    标签: codeigniter


    【解决方案1】:
    $arrTID = $this->input->post('Present');
    $atnDate = $this->input->post('atnDate');
    
    $this->db->select('TraineeID');
    $this->db->where_in('TraineeID', $arrTID);
    $this->db->where('attnDate', $atnDate);
    $query = $this->db->get('tbl_attendance_processed');
    
    // and fetch result
    $res = $query->result(); // as object
    $res = $query->result_array(); // as array
    

    学习这个http://www.codeigniter.com/userguide2/database/active_record.html

    【讨论】:

      【解决方案2】:
      $array_check = array(10,11,12,13);
      $name = "Raj Shekhar";
      
      $this->db->select('*');
      
      $this->db->where_in('id', $array_check);
      
      $this->db->where('name', $name);
      
      $query = $this->db->get('table_name');
      
      // return result as array
      return $query->result_array(); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 2011-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多