-1

A previous developer created a plugin for a website I manage and everything seems to work okay, except I'm getting the error "Missing argument 2 for wpdb::prepare()". I understand that the arguments need to be added, but I'm not clear how to add them in to this specific code, or what arguments to add in this case.

The code is below. Any pointers would be amazing. The error comes up after the fourth closing curly bracket, before "$judges =". It seems also like the query should now be $wpdb->prepare, but again, I'm not clear in this instance how to implement that into this code.

UPDATE:

Apologies, but I am a big bone-head. The code below is the real code from the plugin. I had looked at the wrong line of code previously.

$categories = $wpdb->get_results($wpdb->prepare(sprintf("SELECT c.categoryid as id, c.name, c.public, COUNT(jc.categoryid) as submissions FROM ".$wpdb->prefix."judge_category c LEFT JOIN ". $wpdb->prefix. "judge_submission_category jc ON jc.categoryid = c.categoryid %s GROUP BY c.categoryid %s",$_where, $_order)));

Based on the feedback already provided, I'll see if I can make it work now and I'll post the correct answer once I figure it out, unless anyone else spots it first.

  • The error though is coming from line 20, which is before that line with the $wpdb->get_results(sprintf. I would expect the next error will be at line 21, with that line, but I first need to fix the issue coming from global $wpdb, don't I? – Andrew Dec 12 '13 at 17:43
  • You can check by replacing sprintf by $wpdb->prepare. Can you say whats is the meaning of %%judge%% in your code? – Chittaranjan Dec 12 '13 at 17:46
  • Following my edit, I think I have a fix, but I'm not sure what all of the arguments should be. There's $id and $name, but what about public, categoryid and submissions? Not to mention judge_category, judge_submission_category? – Andrew Dec 12 '13 at 18:36
  • name, public, categoryid are field names in the table id, submissions are column alias judge_category, judge_submission_category are tables. – Chittaranjan Dec 12 '13 at 18:53
1

The error you're facing definitely isn't coming from your code shown above. This is an error notice that was raised with WP 3.5 (IIRC) that should note that stand alone $wpdb->prepare( STATEMENT ); calls help nothing.

Missing argument 2 for wpdb::prepare()

tells you that you haven't added any argument to the call. The prepare() method works like sprintf() aside from the fact that it only knows %s for strings and %d for digits.

So use it like this:

global $wpdb;
$wpdb->query( $wpdb->prepare(
    "SELECT * FROM {$wpdb->posts} WHERE ID = %d AND post_type = %s",
    abs_int( $_GET['post_id'] ),
    esc_attr( $_GET['type' )
) );

Keep in mind that is just dummy code showing how to properly secure HTML form data processed in a WP DB call.

1

EDIT

This is the answer to the corrected question.

$categories = $wpdb->get_results(
                    $wpdb->prepare(
                            "SELECT c.categoryid as id, c.name, c.public, COUNT(jc.categoryid) as submissions
                            FROM ".$wpdb->prefix."judge_category c LEFT JOIN ". $wpdb->prefix. "judge_submission_category jc
                            ON jc.categoryid = c.categoryid %s GROUP BY c.categoryid %s", $_where, $_order
                        )
                    );

PREVIOUS ANSWER BASED ON THE ORIGINAL QUESTION

Change the query to the below

$judges = $wpdb->get_results(
                    $wpdb->prepare(
                            "SELECT u.ID, u.user_login, u.display_name, u.user_nicename, u.user_status, COUNT(c.categoryid) as categories
                            FROM ".$wpdb->prefix."users u LEFT JOIN ". $wpdb->prefix."judge_judge_category c
                            ON c.ID = u.ID WHERE (SELECT umeta_id FROM ". $wpdb->prefix. "usermeta
                            WHERE user_id = u.ID AND meta_key = 'wp_capabilities' AND
                            meta_value LIKE '%%judge%%') > 0 %s GROUP BY u.ID %s",
                            $_where, $_order)
                    );
  • The variables will immediately follow the query within the $wpdb->prepare statement. – Chittaranjan Dec 12 '13 at 18:28
  • Thanks for your help. You definitely had the right idea. Sorry I had posted the wrong code initially. That was very dumb of me. – Andrew Dec 12 '13 at 18:37
  • That won't work either. You can't use prepare to insert whole blocks of SQL like where and order clauses. You need to use prepare to insert variables, and not to build sections of SQL statements piecemeal like this. – Otto Dec 12 '13 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.