Thanks WP 3.5 for the change! What do you do when you get the error: “Missing argument 2 for wpdb::prepare()”
Last Sunday, I was feeling pretty good about my plug-in and my new website over at TheWebReviewer.com. Everything was running smoothly, most of my links where current. The Google bots weren’t complaining too much.
Time to upgrade to Wordpress Version 3.5, right!
So I did that and errors popped up all over the place:
Warning: Missing argument 2 for wpdb::prepare(), called in /home/blah/public_html/wp-content/plugins/reviewpress/blah/my.class.php on line 64 and defined in /home/blh/public_html/wp-includes/wp-db.php on line 990
Well, it seems like WP decided to add a new argument to the prepare() function of Wordpress. (Why they just didn’t make it an optional parameter is beyond me.) Anyway, it really wasn’t a big deal.
First, the prepare function (a part of the database class) is meant to handle escaping, quoting, and int-casting for SQL queries. Its a good clean-up function.
The extra argument(s) are a “smarter” way of inserting variables into the SQL requests. For example:
$wpdb->get_results($wpdb->prepare(’SELECT cat_id FROM “$cat” WHERE pid = 1′));
would now become:
$wpdb->get_results($wpdb->prepare(”SELECT cat_id FROM “%s” WHERE pid = 1″,$cat));
Note that the variable was redefined within the query as a data type and the variable is the second variable parameter.
In most of my cases, I define the query elsewhere:
$mysql = “SELECT cat_id FROM $cat WHERE pid = ‘1′”;
$catId = $wpdb->get_results($wpdb->prepare($mysql)); // BEFORE (Got the Error)
$catId =$wpdb->get_results($wpdb->prepare($mysql,0)); // AFTER (No Error)
Here, I just added a zero to the second argument and everything was happy.
(Still kind of a pain in my butt to go through all of my classes and fix!)
Don't Buy Another Money Making Product Until You Watch These FREE Videos!