Support » Fixing WordPress » Error using $wpdb in a plugin

  • I am trying to create a plug-in which uses a table that I added into my database. The plugin creates a widget which has a multiple choice question form. When the form submits, it goes to my submit.php file which is located in the same plugin folder.

    On the submit.php file, I am trying to run a query to update some values in the table I built by using this:

    $update = $wpdb->update( my_table, array ( guesses=>$numGuesses), array (id=>$id));

    Although I have the “global $wpdb” defined, I still receive “Fatal error: Call to a member function get_results() on a non-object”. The widget successfully uses the $wpdb as it queries the same table to pull the data for the questions (uses $wpdb->get_results()). I don’t understand what I am doing wrong to make this not work, for I have looked over both files and nothing appears to be different.

    Does anybody have any thoughts on what it could be?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Andrew Nevins


    Forum moderator

    Is $wpdb an object?

    I believe it is a class. If you try to echo $wpdb you get an error saying something along the lines of “could not convert to string from class”.

    Moderator Andrew Nevins


    Forum moderator

    Try var dumping it

    When I do the var_dump on the submit page, it returns NULL. But on the page where my query works, it returns:

    object(wpdb)#1 (70) { [“show_errors”]=> bool(false) [“suppress_errors”]=> bool(false) [“last_error”]=> string(0) “” [“num_queries”]=> int(81) [“num_rows”]=> int(2) [“rows_affected”]=> int(1) [“insert_id”]=> int(11096) [“last_query”]=> string(53) ” SELECT * FROM wp_brain_in_gear ” …. with a lot more after that

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Error using $wpdb in a plugin’ is closed to new replies.