2

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( 
7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.