$wpdb Fatal error

2019-08-30 05:47发布

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.

标签: wordpress
3条回答
劳资没心,怎么记你
2楼-- · 2019-08-30 06:18

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');
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-08-30 06:26

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

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-30 06:39

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' );
查看更多
登录 后发表回答