Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter将查询合并到一个循环中_Php_Mysql_Sql_Codeigniter - Fatal编程技术网

Php Codeigniter将查询合并到一个循环中

Php Codeigniter将查询合并到一个循环中,php,mysql,sql,codeigniter,Php,Mysql,Sql,Codeigniter,我有一个ID数组,我正在从数据库中获取这些项目的详细信息。我创建了一个foreach循环来逐个获取每个项,然后在返回控制器之前将其推送到数组中。问题是查询生成器将所有ID组合在一个查询中 public function get_product_by_ids($ids) { $products = array(); foreach ($ids as $id) { $this->db->where('product_id', $id

我有一个ID数组,我正在从数据库中获取这些项目的详细信息。我创建了一个foreach循环来逐个获取每个项,然后在返回控制器之前将其推送到数组中。问题是查询生成器将所有ID组合在一个查询中

public function get_product_by_ids($ids) {
        $products = array();
        foreach ($ids as $id) {
            $this->db->where('product_id', $id);
            $query = $this->db->get('products')->row_array();

            array_push($products, $query);
        }

    return $products;
}
下面是该代码的结果,使用探查器

我的身份证是

Array
(
    [0] => 22
    [1] => 18
    [2] => 21
)
从探查器生成的查询:

SELECT *
FROM `products`
WHERE `product_id` = '22'
AND `product_id` = '18'
AND `product_id` = '21'
AND `product_id` = '22' 

SELECT *
FROM `products`
WHERE `product_id` = '18' 

SELECT *
FROM `products`
WHERE `product_id` = '21' 
输出: 第一个是空的,然后我把第二个换到了最后一个

我尝试将where_in()用于@danblack said,下面是生成的查询:

SELECT *
FROM `products`
WHERE `product_id` = '22'
AND `product_id` = '18'
AND `product_id` = '21'
AND `product_id` IN('22', '18', '21') 
以下是我的新代码:

public function get_product_by_ids($ids) {
        $this->db->where_in('product_id', $ids);
        $query = $this->db->get('products')->result_array();

        return $query;
}

输出:空数组。

我不知道这是CodeIgniter(在CodeIgniter 3.x上)的问题还是我做错了什么,但这里是我的临时解决方案:

public function get_product_by_ids($ids) {
        $newquery = "SELECT * FROM products WHERE product_id IN(";

        $counter = 0;
        foreach ($ids as $id) {
            $newquery .= "'".$id."'";
            if($counter < (sizeof($ids) - 1)) {
                $newquery .= ", ";
            }
            $counter++;
        }

        $newquery .= ")";
        $query = $this->db->query($newquery)->result_array();

        return $query;
}
公共函数通过\u id($id)获取\u产品{
$newquery=“从产品中选择*,产品id位于(”;
$counter=0;
foreach($id作为$id){
$newquery.=“'.”$id.“”;
如果($counter<(sizeof($ids)-1)){
$newquery.=“,”;
}
$counter++;
}
$newquery.=”;
$query=$this->db->query($newquery)->result_array();
返回$query;
}

我不知道这是CodeIgniter(在CodeIgniter 3.x上)的问题还是我做错了什么,但这里是我的临时解决方案:

public function get_product_by_ids($ids) {
        $newquery = "SELECT * FROM products WHERE product_id IN(";

        $counter = 0;
        foreach ($ids as $id) {
            $newquery .= "'".$id."'";
            if($counter < (sizeof($ids) - 1)) {
                $newquery .= ", ";
            }
            $counter++;
        }

        $newquery .= ")";
        $query = $this->db->query($newquery)->result_array();

        return $query;
}
公共函数通过\u id($id)获取\u产品{
$newquery=“从产品中选择*,产品id位于(”;
$counter=0;
foreach($id作为$id){
$newquery.=“'.”$id.“”;
如果($counter<(sizeof($ids)-1)){
$newquery.=“,”;
}
$counter++;
}
$newquery.=”;
$query=$this->db->query($newquery)->result_array();
返回$query;
}
试试这个

public function get_product_by_ids($ids) {
      $this->db->select('*');
      $this->db->from('products');
      $this->db->where_in('product_id', $ids);//$ids should be array
      $query = $this->db->get();
      if ($query->num_rows() > 0) {
         return $query->result_array();
      }else{
         return array();
      }
}
试试这个

public function get_product_by_ids($ids) {
      $this->db->select('*');
      $this->db->from('products');
      $this->db->where_in('product_id', $ids);//$ids should be array
      $query = $this->db->get();
      if ($query->num_rows() > 0) {
         return $query->result_array();
      }else{
         return array();
      }
}

可能重复@danblack我用你的解决方案编辑了代码,但codeigniter仍在添加where子句。只要你不在循环中,danblack的解决方案就会起作用。可能重复@danblack我用你的解决方案编辑了代码,但仍然,codeigniter仍在添加where子句。只要不在循环中,danblack的解决方案就会起作用。