Call to a member function query_db() on a non-object?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scatty1985
Forum Newbie
Posts: 24
Joined: Fri Dec 18, 2009 8:57 am

Call to a member function query_db() on a non-object?

Post by scatty1985 »

Hi,

Im getting the error Call to a member function query_db() on a non-object on the following line of code.

Code: Select all

$db->query_db($q);
From function:

Code: Select all

function e_Check($e) {
        $q = "SELECT email FROM users WHERE email='$e'";
        $db->query_db($q);
        return ($db->q_rows($db->q_result) < 1);
    }
The code from the class is:

Code: Select all

// Query database
    function query_db($query_string) {
        $this->q_result = mysql_query($query_string);
    }
I cant work out why, cant any one give me some advice?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Call to a member function query_db() on a non-object?

Post by Darhazer »

$db is not defined in the function.
You have to instantiate your class.
Or if this is a global object, use

Code: Select all

global $db;
before using the variable. However, using global objects is not a good practice.
scatty1985
Forum Newbie
Posts: 24
Joined: Fri Dec 18, 2009 8:57 am

Re: Call to a member function query_db() on a non-object?

Post by scatty1985 »

Ah I understand...

Code: Select all

function e_Check($e,&$db) {
Instead ;-)
Post Reply