Wordpress: error in getResults

Asked

Viewed 28 times

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 Corretorawp wp-content plugins clients api list.php on line 11

The code of the page 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 is wrong?

1 answer

1

The $wpdb is NULL, which means it doesn’t exist, it wasn’t instantiated.

So the wp-db.php contains only the class class wpdb {...} and nay contains the variable, to use you must include the header, so:

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 necessary 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.