# $wpdb not being defined in function: Fatal error: Call to a member function query() on a non-object For some reason the$wpdb object is not being defined in one particular function. I have four other functions in the file that are using $wpdb and all are working fine except this one simple delete function where I get the error: Call to a member function query() on a non-object... The following is the function in question: function delete_gallery($gallery_id ) {
global $wpdb;$sql = "DELETE from galleries WHERE gallery_id = $gallery_id";$deleted = $wpdb->query($sql );

}


I have a link (delete gallery) on the plugin admin settings page that links to delete-gallery.php that calls the delete function and returns back to the admin page. The contents of that file are:

include 'ga-functions.php';

if (isset($_GET['gallery_id'])) {$gallery_id = $_GET['gallery_id']; delete_gallery($gallery_id);

wp_redirect( $_SERVER['HTTP_REFERER'] ); exit(); }  If I var_dump on the$wpdb after initializing I get the result of NULL. Anyone have any suggestions on what could be causing this in only one particular function like this.

-

Okay, new solution. First off, I'd recommend making the link a submit button and making the name something unique:

<submit name 'del_gallery' />


include 'ga-functions.php';
//checks to see if certain button was pressed
if(isset($_REQUEST['del_gallery'] && isset($_GET['gallery_id']) ) {
$gallery_id =$_GET['gallery_id'];
delete_gallery($gallery_id); wp_redirect($_SERVER['HTTP_REFERER'] );
exit();
}

-
Thanks for your replay Manny. After adding in the hook I'm getting the error: Call to undefined function add_action()... from the delete-gallery.php file –  gzhmr Oct 14 '12 at 5:43
Okay, I see why. You are calling this php file without loading WordPress at all. That's why \$wpdb and add_action are unavailable. I added an include to wp-load.php, which should sole the problem. –  Manny Fleurmond Oct 14 '12 at 12:12
Thanks again. I've tried that using the 'wp-load.php' but when I do it thows the errors/warnings: Use of undefined constant ABSPATH - assumed 'ABSPATH' in C:\xampp\htdocs\wordpress\wp-content\plugins\gallery-admin\includes\delete-galle‌​ry.php on line 3 .....AND..... Fatal error: require_once() [function.require]: Failed opening required 'ABSPATHwp-load.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\wordpress\wp-content\plugins\gallery-admin\includes\delete-galle‌​ry.php on line 3 –  gzhmr Oct 14 '12 at 12:54
okay, give me a sec and let me see what I can find out. By the way, is this php file in a plugin or function file? –  Manny Fleurmond Oct 14 '12 at 13:08
it's in a plugin file. –  gzhmr Oct 14 '12 at 13:14