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

Recommended Answers

All 6 Replies

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.

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

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);
}
?>

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.

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

thank you guys :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.