$wpdb Fatal error

2019-08-30 06:20发布

问题:

I have problems in pointing to the database in Wordpress. I have tried to include global $wpdb and it does not work. And I have also done PHP include for the wp-load.php, wp-db.php, wp-config.php but still no fix.

It says,

Fatal error: Call to a member function get_results() on a non-object in C:\xampp\htdocs\wordpress\wp-content\themes\cv_test\searchresult_details.php on line 47

Sorry, I'm a beginner in Wordpress development. Any help is appreciated. Thanks.

    include_once('http://localhost/wordpress/wp-config.php');
include_once('http://localhost/wordpress/wp-load.php');
include_once('http://localhost/wordpress/wp-includes/wp-db.php');

function retrieveClientDesc()
{
    global $wpdb;

    $query = "SELECT client_desc FROM wp_client WHERE client_name =  'Cal'";
    $result = $wpdb->get_results($query, OBJECT);



    for($i = 0; $i<=count($result); $i++)
    {
        $clientDesc = ($result[$i]->client_desc);
        echo $clientDesc;
    }

    print_r($result);
}

This is part of the codes. It keeps says my $result section has a fatal error.

回答1:

I have had this problem once and this is how i was able to solve it.

global $wpdb, $table_prefix;

if(!isset($wpdb))
{
    //the '../' is the number of folders to go up from the current file to the root-map.
    require_once('../../wp-config.php');
    require_once('../../wp-includes/wp-db.php');
}


回答2:

Thanks to all of you for your help. Managed to do a fix with this though.

include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-load.php' );


回答3:

Replace global $wpdb; with $wpdb = new WPDB;. The wpdb object wasn't yet initiated.



标签: wordpress