Geek Gamer Geek Gamer - 1 month ago 9
PHP Question

WordPress site giving the following error: Missing argument 2 for wpdb::prepare(). Convert?

Problem:
My problem is that, after adding a new plugin to my Wordpress CMS, my site as well as the Wordpress CMS shows a PHP error.

Error: Warning: Missing argument 2 for wpdb::prepare(), called in /home/a4921378/public_html/wp-content/themes/xscholar/framework/includes/core-functions.php on line 433 and defined in /home/a4921378/public_html/wp-includes/wp-db.php on line 1285

Image:
Error Image

Fix:
Can someone please tell me what is wrong and I would be very appreciative if someone converts/fixes this piece of code.

Me:
I'm an amateur coder, only understand the basics and haven't learned PHP yet, I use Wordpress quite often but something like this never came up before. I've been trying to figure out what's wrong but haven't succeeded in 2 hours.

Help:
Any help will be appreciated and would surely stop me from banging my head on my keyboard....

Sites I've Visited:

php-warning-missing-argument-2-for-wpdb-prepare

missing-argument-2-for-wpdbprepare

$terms = $wpdb->get_results($wpdb->prepare( 'SELECT name, slug FROM ' . $wpdb->prefix . 'terms, ' . $wpdb->prefix . 'term_taxonomy WHERE ' . $wpdb->prefix . 'terms.term_id = ' . $wpdb->prefix . 'term_taxonomy.term_id AND taxonomy = "product_cat" ORDER BY name ASC;'));
$wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url));

Answer

You don't have to use $wpdb->prepare for your first query, because the query does not have any placeholder (What a placeholder is?). Just simply use:

$terms = $wpdb->get_results('SELECT name, slug FROM ' . $wpdb->prefix . 'terms, ' . $wpdb->prefix . 'term_taxonomy WHERE ' . $wpdb->prefix . 'terms.term_id = ' . $wpdb->prefix . 'term_taxonomy.term_id AND taxonomy = "product_cat" ORDER BY name ASC;');

Your second query has a placeholder (that %s), but it is wrapped in quotes. It should not be wrapped, the prepare method takes care of it. Just use:

$wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = %s AND wposts.post_type = 'attachment'", $attachment_url));
Comments