Wordpress: error in getResults

0

Good afternoon everyone, I'm having this error in wordpress:

  

Fatal error: Call to a member function get_results () on null in        C: \ xampp \ htdocs \ BrokerWP \ wp-content \ plugins \ clients \ api \ lista.php   online 11

The page code in question:

<?php

$x = require_once '../../../../wp-includes/wp-db.php';


var_dump($x);

global $wpdb;

$sel = "select * from cliente limit 10";
$res = $wpdb->get_results($sel, ARRAY_N);

print_r($res);

What's wrong?

    
asked by anonymous 02.01.2018 / 19:14

1 answer

1

The $wpdb is NULL , ie it does not exist, it has not been instantiated.

So, the wp-db.php only contains the class class wpdb {...} and does not contains the variable, to use you must include the header, like this:

require_once 'wp-blog-header.php';

$sel = "select * from cliente limit 10";
$res = $wpdb->get_results($sel, ARRAY_N);

print_r($res);

Note that global $wpdb; is only required within functions, for example:

require_once 'wp-blog-header.php';

function foobar() {
    global $wpdb;

    $sel = "select * from cliente limit 10";
    $res = $wpdb->get_results($sel, ARRAY_N);

    print_r($res);
}


foo();
    
02.01.2018 / 19:54