database - $wpdb variable throw this error Call to a member function get_results() on a non-object in

I've this error "Call to a member function get_results() on a non-object in"from this code:require_once(

I've this error "Call to a member function get_results() on a non-object in" from this code:

require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] .  $folder . '/wp-load.php');
if (!$wpdb) {
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
} else {
    global $wpdb;
}

function vendorDatails( $param)
{
    if ((substr ($param, 0, 0)) == '#')
    {
        substr($param, 1);
    }

    $order_ids = $wpdb->get_results( 
                                    "
                                     SELECT DISTINCT vendor_id 
                                     FROM $wpdb->lqrhxf_pv_commission 
                                     WHERE order_id = %d
                                    ",
                                        $param
                                );

    foreach ( $order_ids as $order_id )
    {
        echo $order_id->vendor_id;
    }
}

The db parameters are loaded successfully but I've the error on the line above when I use $wpdb variable:

 $order_ids = $wpdb->get_results( 

I've this error "Call to a member function get_results() on a non-object in" from this code:

require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] .  $folder . '/wp-load.php');
if (!$wpdb) {
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
} else {
    global $wpdb;
}

function vendorDatails( $param)
{
    if ((substr ($param, 0, 0)) == '#')
    {
        substr($param, 1);
    }

    $order_ids = $wpdb->get_results( 
                                    "
                                     SELECT DISTINCT vendor_id 
                                     FROM $wpdb->lqrhxf_pv_commission 
                                     WHERE order_id = %d
                                    ",
                                        $param
                                );

    foreach ( $order_ids as $order_id )
    {
        echo $order_id->vendor_id;
    }
}

The db parameters are loaded successfully but I've the error on the line above when I use $wpdb variable:

 $order_ids = $wpdb->get_results( 
Share Improve this question edited Nov 4, 2014 at 15:39 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Nov 4, 2014 at 15:11 GabrieleUGabrieleU 231 gold badge1 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 8

This is a PHP variable scope issue, there is no $wpdb defined in your function.

Add

global $wpdb;

before trying to use the $wpdb object.

Whenever you want to use WordPress' functionality outside of WordPress you can use

include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' ); Then you can use WordPress's functionality. Check This. If you want to just access the database functions then

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' ); require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' ); if (!$wpdb) { $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); } else { global $wpdb; }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365442a4624555.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信