[SOLVED] Passing an array to a query using a WHERE clausee

Issue

This Content is from Stack Overflow. Question asked by Savas çiftci

I want to use array in WHERE clause. But it doesn’t work.

if (count(is_countable($ekkampanyaId) ? $ekkampanyaId : []) > 0) {
        $kampanyadizi = implode("','", $ekkampanyaId);
        $ekkampanyalar = $Panel_Model->urunkampanyalistele($kampanyadizi, date("Y/m/d"));

MY sql side

public function urunkampanyalistele($array , $tarih) {
    $sql = 'SELECT kampanya_ID,kampanya_indirimyuzde FROM flora_kampanya WHERE kampanya_ID IN ("' . $array . '") AND kampanya_aktiflik=1 AND kampanya_baslamatarih<' . $tarih . '<kampanya_bitistarihi';
    return $this->db->select($sql);
}



Solution

I think you are missing some quotes

if (count(is_countable($ekkampanyaId) ? $ekkampanyaId : []) > 0) {
        $kampanyadizi = ",".implode("','", $ekkampanyaId)."'";
        $ekkampanyalar = $Panel_Model->urunkampanyalistele($kampanyadizi, date("Y/m/d"));


This Question was asked in StackOverflow by Savas çiftci and Answered by Rohit Gupta It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?