Site icon RohuTech

How to use MySQL ‘IN’ Operator in prepare statement in WordPress

How to use MySQL 'IN' Operator in prepare statement in WordPress - Long colorful lines of code on a computer screen

How to use MySQL 'IN' Operator in prepare statement in WordPress - Photo by Markus Spiske on Unsplash

This is how to use MySQL IN Operator in prepare statement in WordPress. Let me straight away give you the code.

How to use MySQL ‘IN’ Operator in prepare statement in WordPress – Photo by Kevin Ku on Unsplash
global $wpdb;

$agenda_id_arr = $wpdb->get_results( $wpdb->prepare( 'SELECT agenda_id 
                 FROM ' . $agenda_bookmark_tbl . ' WHERE user_id = 
                 %d', $user_id ), "ARRAY_A" );

foreach( $agenda_id_arr as $agenda_id ){
    $agenda_ids[] = $agenda_id['agenda_id'];
}

$in = implode(',', array_fill(0, count($agenda_ids), '%d'));

$agenda_list_arr = $wpdb->get_results( $wpdb->prepare( ' SELECT * FROM 
                   ' . $agenda_tbl . ' WHERE id in ('. $in .') ', 
                   $agenda_ids), "ARRAY_A" );

Listed a few important links to refer.

Was that a useful Article? Let me know your comments & feedback. Do I need any edits in the code?

Other Popular Reads

Exit mobile version