简体   繁体   中英

Codeigniter where_in JSON column

i need a Codeigniter query to get results from a JSON column MYSQL table.

Sample column data bellow.

["1","2","6","4","3","5","7","9","8"]

Currently im using bellow code for this but it's not working.

$DOCUMENTS = $this->db->select('*')->where('JSON_CONTAINS(BELONG) = '.$_POST['ID'].'')->get('SOLIDARITY_REQUESTED_FILES')->result_array();

If your JSON formatted as array like your data, you can concatenate the ID to the commas and quotes. then TRIM the square bracket and find by LIKE statement.

$concat = "CONCAT(',\"',TRIM(LEADING '[' FROM TRIM(TRAILING ']' FROM BELONG)),'\",')";
$theId = ',"'.$_POST['ID'].'",';
$DOCUMENTS = $this->db->select('*')->like($concat, $theId)->get('SOLIDARITY_REQUESTED_FILES')->result_array();

Hope this help.

Suppose i'm doing to get record where city_id should be in following list. 1,2,3,4,5,6,7,8 then following line you can add into your code

    $city_id_list = "1,2,6,4,3,5,7,9,8";
    $this->db->where_in('city_id', $city_id_list);

JSON_CONTAINS function required two minimum arguments. check more details HERE

It is dangerous to add $_POST data into query. Codeigniter having input class to fetch data from POST request.

$id = $this->input->post('ID');

$DOCUMENTS = $this->db->select('*')->where("JSON_CONTAINS(BELONG, $id)>0")->get('SOLIDARITY_REQUESTED_FILES')->result_array();

Feel free if you need further guidance.

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