Reputation: 92
I have problems in pointing to the database in Wordpress. I have tried to include global $wpdb and it does not work. And I have also done PHP include for the wp-load.php
, wp-db.php
, wp-config.php but still no fix.
It says,
Fatal error: Call to a member function get_results() on a non-object in C:\xampp\htdocs\wordpress\wp-content\themes\cv_test\searchresult_details.php on line 47
Sorry, I'm a beginner in Wordpress development. Any help is appreciated. Thanks.
include_once('http://localhost/wordpress/wp-config.php');
include_once('http://localhost/wordpress/wp-load.php');
include_once('http://localhost/wordpress/wp-includes/wp-db.php');
function retrieveClientDesc()
{
global $wpdb;
$query = "SELECT client_desc FROM wp_client WHERE client_name = 'Cal'";
$result = $wpdb->get_results($query, OBJECT);
for($i = 0; $i<=count($result); $i++)
{
$clientDesc = ($result[$i]->client_desc);
echo $clientDesc;
}
print_r($result);
}
This is part of the codes. It keeps says my $result section has a fatal error.
Upvotes: 0
Views: 6928
Reputation: 161
check your dir of the file and the pass your host credentials in WPDB($dbuser, $dbpassword, $dbname, $dbhost)
include_once( __DIR__. '/../../../../wp-config.php');
include_once( __DIR__. '/../../../../wp-load.php');
include_once( __DIR__. '/../../../../wp-includes/wp-db.php');
class Myclass{
function anyname(){
$wpdb = new WPDB('root', '', 'wp_hopper', 'localhost');
$results = $wpdb->get_results( "SELECT * FROM table", OBJECT );
print_r($results) ;
}
}
Upvotes: 0
Reputation: 1
Remove parameter object from $wpdb->get_results( $query );
and try with the code bellow
global $wpdb;
$table = "{$wpdb->prefix}client"
$query = "SELECT client_desc FROM wp_client WHERE client_name = 'Cal'";
$result = $wpdb->get_results( $query );
Upvotes: 0
Reputation: 92
Thanks to all of you for your help. Managed to do a fix with this though.
include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-load.php' );
Upvotes: 1
Reputation: 4176
I have had this problem once and this is how i was able to solve it.
global $wpdb, $table_prefix;
if(!isset($wpdb))
{
//the '../' is the number of folders to go up from the current file to the root-map.
require_once('../../wp-config.php');
require_once('../../wp-includes/wp-db.php');
}
Upvotes: 2
Reputation: 4690
Replace global $wpdb;
with $wpdb = new WPDB;
. The wpdb object wasn't yet initiated.
Upvotes: 0