Support » Developing with WordPress » Call to a member function get_results() on null

  • Hello everyone,
    I need to access a table in my database from a php file. I begin my PHP file with global $wpdb; and try a simple search $myrows = $wpdb->get_results( "SELECT * FROM my_table" );
    I get the following error : Uncaught Error: Call to a member function get_results() on null as if my table was unknown.
    I tried with other tables and had the same result.
    Do you have any clue why it does not work ?
    Thanks

    The “whole” code :

    <?php
    global $wpdb;
    $myrows = $wpdb->get_results( "SELECT id FROM db_rcp_payments" );
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • I believe that the issue is the global $wpdb is null and this is an aribitrary PHP file outside of WordPress. The PHP file needs to be related to WordPress in some way or another. If you want to use WordPress related functionality in arbitrary PHP files you need to include the WordPress load process that initializes all the WordPress globals and constants which is handled via wp-load.php in the root WordPress folder. If you include wp-load.php you should have access to $wpdb.

    Of you can skip this include and create a simple MU Plugin or normal Plugin

    Thank you for your answer. I cannot include wp-load.php. I have no error but if I add a echo 'hello';, it does not appear. I included the right path, I can’t understand why it does not work…

    That’s a question that’s difficult to answer. wp-load.php file location is going to be entirely dependent on both: Where you have WordPress installed ( root or subfolder ) and where your PHP file is that is trying to include the wp-load file.

    You may be better off doing whatever it is you’re trying to do in a MU Plugin file or as a separate plugin itself.

    Thanks to FileZilla I know where my files are. Usually, when it does not find the file I’m looking for, I have an error saying that the file does not exist on the specified folder. When I try to include wp-load, I have no error, just nothing executes after this include. There might be a question af permissions or sthg like that but I don’t know what…
    I cannot access my database table in a plugin because my PHP file needs to be called in a URL (at least I don’t know how to specify a fixed URL inside a plugin).
    Thank you !

    Moderator bcworkz

    (@bcworkz)

    Including wp-load.php is not ideal anyway because the location of the file relative to plugins or themes can vary by installation. If you need a direct access URL for your code, one option is to run the request through /wp-admin/admin-post.php. It works similar to Ajax, but no JavaScript is required. It also works for GET requests as well as POST. You do have to provide an action parameter, which is used to create an action tag to which you add your code as a callback.

    If you look at the source code of the file it’s fairly clear what it does and how you interface with it, assuming you are familiar with PHP and WP action hooks. Not everyone is, and that’s OK too.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.