简体   繁体   English

WHERE_IN仅在codeigniter中返回第一个匹配项

[英]WHERE_IN returns only first match in codeigniter

I am trying to get a list of coupons through ajax when the checkboxes are selected. 选择复选框时,我试图通过ajax获取优惠券列表。 So everything else is working fine but the query is returning only the first match. 因此,其他所有工作都很好,但查询仅返回第一个匹配项。

So my query is: 所以我的查询是:

$this->db->from('tbl_coupons');
        if($storeids !=''){
         $ids = array($storeids);
$this->db->where_in('coupon_store', $ids );
        }       
        $this->db->where('coupon_cat', $catid);
        $this->db->where('coupon_status', 'active');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            $ds = $query->result_array();}

According to this my SQLquery becomes 据此,我的SQLquery成为

SELECT * FROM `tbl_coupons` 
WHERE `coupon_store` IN('1,97') 
AND `coupon_cat` = '16' 
AND `coupon_status` = 'active'

But this query is returning values with coupon_store=1 and no results are coming for coupon_store=97 但是此查询返回的值带有coupon_store=1而没有结果coupon_store=97

I checked values for coupon store 97 which exists in that category. 我检查了该类别中存在的优惠券商店97的值。

use below way if data exist it will be part of query. 如果数据存在,将以下面的方式使用它将成为查询的一部分。

        storeids = explode(',',storeids);
        $ids = array();
        foreach($storeids as $val){
            $ids[] = $val;
        }
        if(!empty($ids)){
            $this->db->where_in('coupon_store', $ids );
        }

hope it will create proper sql query 希望它将创建正确的SQL查询

The query is mostly correct, except at line 2, where you need to make the change as: 该查询大部分是正确的,除了在第2行,您需要按以下方式进行更改:

WHERE coupon_store IN('1','97')

everything else remains the same. 其他一切都保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM