Non-object error, how do I fix it?

I keep getting an error:

but I’m not sure exactly where the problem is or how to fix it. Line 31 is:

if( $GLOBALS['dbdebug'] ) {

and the full code is:

function usedb( $dbname,$dblink ) { if( $GLOBALS[$dblink]->select_db( $dbname ) ) { $GLOBALS['dbPtrInt']=$dbname; } else { if( $GLOBALS['dbdebug'] ) { die( 'Failed to change database ('.$GLOBALS[$dblink]->connect_errno.') '.$GLOBALS[$dblink]->connect_error ); exit(); } else { logdberr( 'Failed to change database...','Connect Error ('.$GLOBALS[$dblink]->connect_errno.') '.$GLOBALS[$dblink]->connect_error ); header( "Location: /error/dberror.php" ); exit(); } } }

I’d be grateful for any help on sorting this please. The code works perfectly on a live site but when I try to integrate it with WordPress I get the error.

The error means that whatever is in $GLOBALS[$dblink] (if anything) is not an object, so somewhere outside of this function $GLOBALS[$dblink] is not being set correctly.

I’m sorry to be so dumb but how do I correct this? I can’t see where $GLOBALS[$dblink] is being set. This is all I can see:

[code]### change database
function usedb( $dbname,$dblink ) {
if( $GLOBALS[$dblink]->select_db( $dbname ) ) {
$GLOBALS[‘dbPtrInt’]=$dbname;
} else {
if( $GLOBALS[‘dbdebug’] ) {
die( ‘Failed to change database (’.$GLOBALS[$dblink]->connect_errno.') ‘.$GLOBALS[$dblink]->connect_error );
exit();
} else {
logdberr( ‘Failed to change database…’,‘Connect Error (’.$GLOBALS[$dblink]->connect_errno.’) '.$GLOBALS[$dblink]->connect_error );
header( “Location: /error/dberror.php” );
exit();
}
}
}

database select

function dbselect( $query,$dblink ) {
if( $result=$GLOBALS[$dblink]->query( $query ) ) {
while( $row=$result->fetch_array( MYSQLI_ASSOC ) ) { $dbdata=$row; }
if( !isset( $dbdata ) ) { $dbdata=false; }
if( is_object( $result ) ) {
$result->close();
}
return( $dbdata );
} else {
if( $GLOBALS[‘dbdebug’] ) {
die( ‘Query failed:

’.$query.‘

(’.$GLOBALS[$dblink]->error.‘)’ );
exit();
} else {
logdberr( $query,‘Query failed (’.$GLOBALS[$dblink]->error.‘)’ );
header( “Location: /error/dberror.php” );
exit();
}
}
}

database update/insert/delete

function dbupdate( $query,$dblink ) {
if( $result=$GLOBALS[$dblink]->query( $query ) ) {
if( substr( $query,0,6 )==“insert” ) {
if( is_object( $result ) ) {
$result->close();
}
return( $GLOBALS[$dblink]->insert_id );
} else {
if( is_object( $result ) ) {
$result->close();
}
return false;
}
} else {
if( $GLOBALS[‘dbdebug’] ) {
die( ‘Query failed:

’.$query.‘

(’.$GLOBALS[$dblink]->error.‘)’ );
exit();
} else {
logdberr( $query,‘Query failed (’.$GLOBALS[$dblink]->error.‘)’ );
header( “Location: /error/dberror.php” );
exit();
}
}
}[/code]

It’s likely that $GLOBALS[$dblink] is being set in an included file.

I’ve been through all of the linked files and the only file that references $GLOBALS[$dblink] at all is this one.

See what value $dblink has then search for it.

Then your code is correct. $GLOBALS[$dblink] is not set, so it’s not an object.

I assume you copy and pasted this code from somewhere?

The only reference to $dblink outside of $GLOBALS is this:

function usedb( $dbname,$dblink ) { if( $GLOBALS[$dblink]->select_db( $dbname ) ) { $GLOBALS['dbPtrInt']=$dbname; } else { if( $GLOBALS['dbdebug'] ) { die( 'Failed to change database ('.$GLOBALS[$dblink]->connect_errno.') '.$GLOBALS[$dblink]->connect_error ); exit(); } else { logdberr( 'Failed to change database...','Connect Error ('.$GLOBALS[$dblink]->connect_errno.') '.$GLOBALS[$dblink]->connect_error ); header( "Location: /error/dberror.php" ); exit(); } } }

It’s my first day in my new job and I’m trying to get the dynamic header from out site to work with WordPress. When I try I get just a blank page with that error. On the website itself there’s no errors and it works no problems.

You’re not looking at the right thing. The $dblink is passed in. So if you have this…

The call would be like this: usedb(‘dbName’, ‘dbLink’)

That means at somepoint, then $GLOBALS[‘dbLink’] has been set

I tried changing that, but got this error:

I’m sorry I’m probably being extremely dumb but I’ve not used php that much before

Where are you calling the function usedb, and what variables are you putting into it?

I’m sorry I don’t quite understand. That function is called on lots of files/pages and is used to load the database. As I say this is my first day in the job so I’m still trying to find my way round and where different functions are.

Wow. I totally misread your original post.

To resolve this issue, where ever your globals are being set, add this line:

$GLOBALS['dbdebug'] = true;

Then when you’re done, change it to this (do NOT remove it)

$GLOBALS['dbdebug'] = false;

It looks like the code was written with some rudimentary debug logging added in, but is only used when a global flag is set. Then when it got moved to production, someone removed the global value set instead of just setting it to false.

Do I add it above or below where globals is? Should it be something like this:

function usedb( $dbname,$dblink ) {
    $GLOBALS['dbdebug'] = true;
    if( $GLOBALS[$dblink]->select_db( $dbname ) ) {
      $GLOBALS['dbPtrInt']=$dbname;
    } else {

Also this is at the top of the db.inc file:
### debugging 1=on 0=off
$dbdebug=0;

Wow. I should have just stayed out of this thread. Your OP is a little misleading. Your initial post referenced a line number which was the debug, which is what my last reply was to. But your line counts are obviously off (are you using an IDE which provides line numbers?), since the error is on the select_db() line.

OK, the error is a mismatch somewhere along the way. You’re getting an error because the $GLOBALS[$dblink] doesn’t exist in the context being run.

So somewhere in your code, you’ve got a call to a database which doesn’t exist. In other words, you’ve got a call which is something like this:

usedb('TableName', 'ThisIsMyLink');

For that to work you’ve got to have something like one of these two lines occur BEFORE the call to the usedb method:

$ThisIsMyLink = new mysqli("localhost", "my_user", "my_password", "test");

or

$GLOBALS['ThisIsMyLink'] = new mysqli("localhost", "my_user", "my_password", "test");

It’s just been suggested that I include the db.inc file first then use 'usedb( “blog”,“dbLinkInt” ); it should work, but I really don’t know which part I should change? Sorry to ask such dumb questions but this is my first day in the job and I’ve very new to php

Also the file is used as part of the cms for the site so I’m worried about changing too much

If the db.inc file is where the database configuration is stored, then yes it probably will work - that should have been one of the first instructions your co-workers gave you before throwing you to the wolves.

I’m sorry but how do I do that? I think that they think I’m a better coder than I really am and I’m now getting really worried about this

You have to use either

include 'filename';

or

require 'filename';

Just a thought - do you know PHP at all? If not, you’re REALLY going to do some studying to get your feet underneath you. This command is basic, php 101.