0

I have a code:

  <body>   
        <?php
        global $wpdb;

        $sql = "";
        $sql = "SELECT * FROM emgAdmin";
        $sql = "$sql INNER JOIN wp_posts ON wp_posts.ID = emgAdmin.ID_OfPost ";        
        $ArrResult = $wpdb->get_results($sql);

        while ($emgAdminResult = mysql_fetch_object($ArrResult)) {
            echo($emgAdminResult->post_date);
        }
        ?>
    </body>

and I'm getting an error that says,"Call to a member function get_results() on null" ...
I have tested the sql and it did worked, rendering all that data that I wanted.

I dont know why I'm getting the error with the get_results() function of wordpress when I have already defined the global $wpdb

3
Contributors
6
Replies
21
Views
2 Years
Discussion Span
Last Post by Kewne
0

If it says the variable $wpdb is null then it's null.
I assume you are setting $wpdb somewhere else. You say you've defined it but have actually instantiated it?
You'd need to have a look at that code then as that is where your troulbe most likely lies.

0

I made a link that redirects to this php file....
so what I did is to set "global $wpdb" again to retrieve data from the table.
and thats the error I'm getting...

Edited by Kewne

1

Get rid of global $wpdb and then add this at the top of your php/file

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );

Like so

<?php
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );

$sql = "";
$sql = "SELECT * FROM emgAdmin";
$sql = "$sql INNER JOIN wp_posts ON wp_posts.ID = emgAdmin.ID_OfPost ";        
$ArrResult = $wpdb->get_results($sql);

while ($emgAdminResult = mysql_fetch_object($ArrResult)) {
    echo($emgAdminResult->post_date);
}
?>
0

Thank you for the replies. I have change my code as what you've said pixelsoul
But that's weird that I couldn't get any result from the query I made.
When testing the query under myPHPAdmin, it works and retrieves data. But running my php file here, I get no results.

0

ok i think I got it...

from my

while ($emgAdminResult = mysql_fetch_object($ArrResult)) {
    echo($emgAdminResult->post_date);
}

i changed it to

foreach ($ArrAdminResult as $sampleResult) { 
            echo($sampleResult->post_title);
        }

and got the results i want..since the get_result returns an array...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.