Calling $wpdb in a function in non-WordPress file

Playing around with the query SHOW TABLE STATUS in a non-WordPress core/plugin/template file using wp-load.php to access WordPress functions and database connection.

require_once('wp-load.php');

function mysqlInfo() {
    $info = $wpdb->get_results('SHOW TABLE STATUS');
    foreach($info as $table) {
        echo $table->name;
    }
}

I keep running into this error:

Read More

Fatal error: Call to a member function get_results() on a non-object in /../../../file.php

I have tried declaring $wpdb as a global variable and tried creating a new class out of $wpdb

Related posts

Leave a Reply

1 comment

  1. You have to declare the global variable within the function

    function mysqlInfo() {
    global $wpdb;
    $info = $wpdb->get_results('SHOW TABLE STATUS');
    foreach($info as $table) {
        echo $table->Name;
    }}