Not understanding why I’m getting this error. I’ve called global $wpdb within the script.
This is the line generating the error:
$existing_job = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM " . $wpdb->prefix ."postmeta WHERE meta_key = 'external_job_ad_id' AND meta_value = %d", $external_job_ad_id));
Do you have an instance of the $wpdb object active? The error means that php can’t find an object called $wpdb you need to do:
$wpdb = new object_name();
where object_name is the name of the class.
Last I knew, $wpdb was set in a core file. My guess is there’s a file not found for whatever reason - spelling, permission, etc. - or the code failed to declare $wpdb as global.
Yes, $wpdb is setup in the core files and I should be able to call it with only:
global $wpdb;
I’m using wp 3.2.1. I still haven’t found any answers to why this isn’t working. It’s all fairly textbook. Are the any known issues with using globals in the current wp version?
Not that I know of. I wonder if it’s something else (the prepare?) and it’s giving you a misleading error message.
A pain, but maybe if you break it down and test as you build it back up you can find something. eg.
global $wpdb;
echo $wpdb->prefix;
just to see if you still get the error mesage.
Yeah, I tried that. Thanks for the suggestion though. I think what it might be is I access the script by typing the entire filepath into the url (ex. localhost/examplesite/includes/themes/mytheme/libs/script.php), rather than having wp call it through page functionality.
Yes, that’s it. WordPress loads a bunch of settings eg. DEFINE()s, when the index file opens.
You might be able to add an include or require for includes/wp-db.php (and probably more needed files) but I think it would be easiest and best to run it by opening the blog and letting it do it’s thing.