WordPress.org

Ready to get started?Download WordPress

Forums

Error using $wpdb in a plugin (5 posts)

  1. bahumba
    Member
    Posted 1 year ago #

    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?

  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    Is $wpdb an object?

  3. bahumba
    Member
    Posted 1 year ago #

    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".

  4. Andrew
    Forum Moderator
    Posted 1 year ago #

    Try var dumping it

  5. bahumba
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic