【问题标题】:using array in where condition在 where 条件下使用数组
【发布时间】:2020-04-01 09:51:06
【问题描述】:

我有一个名为$req_dep 的数组数据。这是我var_dump()它时的样子。

array(2) { [0]=> int(41) [1]=> int(765) }

我想在查询中使用该数据

  select * from dbo.RequisitionTable 
  where RequestorID = '$ID'
  and RequestorDepartmentID in ($req_dep)
  and IsProcessedToHire=0 
  and IsHold = 0
  and IsRejected = 0

但它总是得到错误"Array to string conversion"。像这样的where条件下如何使用数组?

谢谢

【问题讨论】:

  • 内爆它,或者更好的是使用codeigniter的活动记录并使用$this->db->where_in()
  • 这能回答你的问题吗? Array to String PHP?
  • @Kevin idea 对我有帮助

标签: php sql sql-server codeigniter


【解决方案1】:

您的查询:

select * from dbo.RequisitionTable 
where RequestorID = '$ID'
and RequestorDepartmentID in ($req_dep)
and IsProcessedToHire=0 
and IsHold = 0
and IsRejected = 0

可以像这样轻松转换为 CodeIgniter 的查询生成器:

$this->db->select('*');
$this->db->from('dbo.RequisitionTable');
$this->db->where('RequestorID', $ID);
$this->db->where_in('RequestorDepartmentID', $req_dep);
$this->db->where('IsProcessedToHire', 0);
$this->db->where('IsHold', 0);
$this->db->where('IsRejected', 0);

以上假设 $ID 和 $req_dep 被传递给模型并且后者是一个数组

【讨论】:

    【解决方案2】:

    为什么不使用 Query builder class ?你可以这样做,

    $conds = [
       "RequestorID" => $ID,
       "IsProcessedToHire" => 0,
       "IsHold" => 0,
       "IsRejected" => 0  
    ];
    
    $result = $this->db->where($conds)->where_in("RequestorDepartmentID", $req_dep)->get("RequisitionTable")->result_array();
    

    【讨论】:

      猜你喜欢
      • 2012-04-14
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2018-01-31
      • 2014-04-21
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多