Welcome Guest, Not a member yet? Register   Sign In
whereNotIn query builder - empty array
#1
Wink 

I have an issue whilst trying to exclude courses which are already stated under another heading. Using the following code i am getting the error
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')
AND `events`.`event_deleted_at` IS NULL
ORDER BY `event_user_valid_to` DESC' at line 6
Code:
$this->data['valid_courses'] = $this->event->join('courses', 'courses.course_id = events.course_id')->join('events_users', 'events.event_id = events_users.event_id')->where('event_user_valid_to <=', $today)->orderBy('event_user_valid_to', 'DESC')->findAll();
        $valid_courses_ids  = array_map(function($n) {return $n['course_id'];}, $this->data['valid_courses']);
        $this->data['cid'] = $valid_courses_ids;
        //$valid_courses_ids = [1, 2, 3];

        $this->data['expired_courses'] = $this->event->join('courses', 'courses.course_id = events.course_id')->join('events_users', 'events.event_id = events_users.event_id')->where('event_user_valid_to >=', $today)->whereNotIn('events.course_id', $valid_courses_ids)->orderBy('event_user_valid_to', 'DESC')->findAll();

As you can see the
Code:
$valid_courses_ids
is empty and creates the error but when not empty it doesn't. Am I supposed to just check for such an occurance or am I right in thinking that this should not be aan issue?

Sorry if this is something that you feel is a waste of time but It seemed qquite unusual to me.

Regards,
Jonsmith1982
Reply
#2

(This post was last modified: 06-15-2022, 05:17 PM by kenjis.)

If the array is empty, it seems the following SQL that is syntax error would be produced.

PHP Code:
$names = [];
$builder->whereNotIn('username'$names);
// Produces: WHERE username NOT IN () 

https://codeigniter4.github.io/CodeIgnit...wherenotin
Reply




Theme © iAndrew 2016 - Forum software by © MyBB