WPDB::Prepare Warning in WordPress 3.5

I just today upgraded the jMonkeyEngine site to WordPress 3.5 and noticed that one of our plugins was causing errors.  The exact error looks like Missing argument 2 for wpdb::prepare().  Looking at the wp-db.php file that ships with WordPress 3.5, we can see that there is indeed a second argument and it is documented as an array for options:

@param array|mixed $args The array of variables to substitute into the query’s placeholders if being called like
{@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query’s placeholders if being called like {@link http://php.net/sprintf sprintf()}.

Cool, so let’s change the code that calls the prepare method to have an empty array as a second argument:

<?php
// Old (single argument) function call
global $wpdb;
$authorIDs = $wpdb->get_col($wpdb->prepare(“SELECT post_author FROM ” . $wpdb->posts . “ WHERE ID = ” . intval($postID) . “ LIMIT 1”);

// New (two argument) function call global $wpdb; $authorIDs = $wpdb->get_col($wpdb->prepare(“SELECT post_author FROM ” . $wpdb->posts . “ WHERE ID = ” . intval($postID) . “ LIMIT 1”, array())); ?>

If your queries were previously working, they will indeed continue to wok with the addition of this simple fix (though you may want to investigate using the functionality provided by WP in this new functionality)

Comments