Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
OOP Problem - Call to a member function on a non-object
Old 05-22-2005, 07:30 AM OOP Problem - Call to a member function on a non-object
Super Talker

Posts: 121
Trades: 0
I am trying to write a class that will allow me to add a member to my users database change tehir password log them in and out, i am using the PEAR DB class to connect to my database, i am getting this error when i run the script, i am trying to login:

Fatal error: Call to a member function on a non-object in /home/nanothre/public_html/classes/member.class.php on line 22

My member class is here:(I have put a comment next to line 22)
PHP Code:
class member {

    var 
$db;
    var 
$databaseTable;
    var 
$username;
    var 
$password;
    var 
$result;
    
    function 
login($username$password){
        if(!
get_magic_quotes_gpc()){
            
$password addslashes($password);
            
$username addslashes($username);
        }
        
$password md5($password);
        
$sql "SELECT id, username, password FROM $databaseTable WHERE username = '$username' AND password = '$password'";
        
$this->result $db->query($sql); //<<<<<<<<<<<<<<<<LINE 22
        
if (DB::isError ($result)){
            die(
'There was an error while processing the query: '$result->getMessage());
        }
        if(
$result->numRows() > 0){
            
$row $result->fetchrow(DB_FETCHMODE_ASSOC);
            
$_SESSION['id'] = $row['id'];
            
$_SESSION['username'] = $row['username'];
            
$_SESSION['password'] = $row['password'];
            
print_r($_SESSION);
        } else {
            return 
false;
        }
        
$result->free();
    }
    
    function 
logout(){
        
session_destroy();
    }    
    
    function 
newMember($password$username$email){
        if(!
get_magic_quotes_gpc()){
            
$password addslashes($password);
            
$username addslashes($username);
            
$email addslashes($email);
        }
        
$sql "INSERT INTO $databaseTable(username, email, password) VALUES('$username','$email','$password')";
        
$result $db->query($sql);
        if (
DB::isError ($result)){
            die(
'There was an error while processing the query: '$result->getMessage());
        }
    }

After the username and password have been submitted this is what is called by the script:

PHP Code:
    $member = new member;
    
$member->databaseTable 'test_users';
    
$member->db $db;
    
$member->login($_POST['username'], $_POST['password']); 
Can anyone explain what is wrong?
vegancoder is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-22-2005, 12:07 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
You're trying to use a $db object that doesn't exist. You need to reference class member variables with the $this keyword.

PHP Code:
$this->result $this->db->query($sql); 
Which means you also have a couple other lines to correct (working with $result and another with $db in the newMember() method).
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 05-22-2005, 12:59 PM
Super Talker

Posts: 121
Trades: 0
thanks alot chroder, ill try that now then, i'm pretty new to OOP
vegancoder is offline
Reply With Quote
View Public Profile
 
Old 05-22-2005, 01:45 PM
Super Talker

Posts: 121
Trades: 0
How come i dont have to use the $this referance with this line:
PHP Code:
$password md5($password); 
vegancoder is offline
Reply With Quote
View Public Profile
 
Old 05-22-2005, 02:01 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
$this only refers to the class member methods (functions) and properties (variables). Since $password was passed to the function, it became a variable in the local scope to the function.

Check out variable scope and the section on the $this pointer in classes and objects.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to OOP Problem - Call to a member function on a non-object
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32169 seconds with 12 queries