[wp-trac] [WordPress Trac] #25559: Enhance prepare method to better support SQL IN operator

WordPress Trac noreply at wordpress.org
Mon Oct 14 16:15:18 UTC 2013


#25559: Enhance prepare method to better support SQL IN operator
------------------------------------------------+--------------------------
 Reporter:  cannona                             |       Owner:
     Type:  enhancement                         |      Status:  new
 Priority:  normal                              |   Milestone:  Awaiting
Component:  Database                            |  Review
 Severity:  normal                              |     Version:  trunk
 Keywords:  has-patch 2nd-opinion dev-feedback  |  Resolution:
------------------------------------------------+--------------------------

Comment (by johnbillion):

 I'm coming around to the idea of flattening all the arguments, because it
 means arrays and argument lists would both be supported. I can see that
 being a nice-to-have, but I'm still not sure if it has a real-world use.

 With [attachment:wp-db_mods.patch] in place, you'd need to count the
 number of items in your array and then insert the count into the
 conversion specification like so:

 {{{
 $count = count( $array );
 $sql = $wpdb->prepare( "
         SELECT *
         FROM table
         WHERE field IN ( %#{$count}s )
 ", $array );
 }}}

 It works, but it muddies the format quite a bit. You're inserting a
 variable into a conversion specification in order to convert a second
 variable into the format you need. Better:

 {{{
 $sql = $wpdb->prepare( "
         SELECT *
         FROM table
         WHERE field IN ( %#s )
 ", $array );
 }}}

 I'm going to give this some more thought.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/25559#comment:5>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list