I’m currently trying to write a query in codeigniter, I’m having a problem including and implode inside the query. This is my code.
$permitedCodeList = ["ELEC-22", "ELEC-100", "ELEC-200", "999873"]; $query = $this->inventario_db->query(" SELECT * FROM pt_inv_num_id_equipo WHERE numID = '123456789' AND codigo IN ('" . implode("','", $permitedCodeList). "') LIMIT 1 "); $result = $query->row();
I’m getting the following error message: Unknown column 'ELEC' in 'where clause'
. I know it has todo with the implode returning the data as ELEC-22,ELEC-100,ELEC-200,999873
but it doesn’t seem to add a single quote to each value and include it in the query.
Any help is greatly appreciated.
Advertisement
Answer
It would be better if you could use CI Query Builder
It specifically has where_in
method to handle such scenario.
Your query can be constructed as below:
$permitedCodeList = ["ELEC-22", "ELEC-100", "ELEC-200", "999873"]; $this->db->from('pt_inv_num_id_equipo'); $this->db->where('numID ', 1234 ); $this->db->where_in('codigo', $permitedCodeList ); $result = $this->db->get()->row_array();
You could also debug the last query using below code
echo $this->db->last_query(); die();