简体   繁体   中英

Getting incorrect query in codeigniter

I am passing variable value as $id="'10','11',12'" from view to model through controller, when I fetch the records from mysql table using above $id value as:

$query=$this->db->query('select * from userdetails where Id IN ($ids)')

I am not getting results, but it prints the query as:

SELECT * FROM (`userdetails`) WHERE `Id` IN ('\'10\',\'11\',\'12\'')

How to solve this problem?

Ok Try this :

$this->db->escape($id);
$query=$this->db->query("select * from userdetails where Id IN ($ids)");

Use stripslashes($id); before passing in the query. Or mysql_real_escape_string() should also work.
In this way :

$ids = stripslashes($ids);
$query=$this->db->query("select * from userdetails where Id IN ($ids)");

Or you can send the value in comma separated in this way $ids = "11,12,13"; and it should work.

Send the ids in an array...

$id_array = array('10','11','12');

Model:

//implode converts your array into a string without quotes
$comma_separated = implode(",",$id_array);
$query=$this->db->query('select * from userdetails where Id IN ($comma_separated)');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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