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)

4 Comments

  1. DopeFuse December 12, 2012

    ur the fucking man

  2. Ash Bryant December 21, 2012

    Sorry, but where do you make this change?

  3. Skye December 22, 2012

    You make this change in the plugin that is throwing the error.

    Warning: Missing argument 2 for wpdb::prepare(), called in /var/www/wp-content/plugins/foo/bar.php on line 292 and defined in /var/www/wp-includes/wp-db.php on line 990

    Using this example, you would be making the change on line 292 in “/var/www/wp-content/plugins/foo/bar.php”

  4. viniciusantos December 25, 2012

    Thank you a lot for the information!

Leave a Reply