Search code examples
phpcodeignitercodeigniter-3codeigniter-query-builder

How to delete an array present inside where_in condition in codeigniter?


I have where_in condition $this->db->where_in('student_id',$arr); Now I want to delete the contents present in $arr from a table using this code $this->db->delete('top_students'); But I got a database error

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2 DELETE FROM top_students WHERE student_id IN()"

How to solve this?


Solution

  • Hope this will help you :

    Check for empty like this ;

    if ( ! empty($arr))
    {
       $this->db->where_in('student_id',$arr);
       $this->db->delete('top_students'); 
    }
    

    For more : https://www.codeigniter.com/user_guide/database/query_builder.html#deleting-data