troubleshooting Question

Call to a member function on a non-object

Avatar of fosiul01
fosiul01Flag for United Kingdom of Great Britain and Northern Ireland asked on
PHP
4 Comments1 Solution240 ViewsLast Modified:
HI
If you see the bellow code, there are 2 types of code i have attached


default.php (workable code) and (UserAuthentication.php and index.php)->need help

In default.php , i included couple of class for mysql connection and query. so when i run this default.php, the out put shows : Connected to Database and table name

now come to Userauthentication.php and index.php

only difference between default.php and (userAuthentication.php and index.php) is i put bellow code seperately in UserAuthentication.php
 if($db->getDb() != ""){        
        echo "Connected to the Server<br>";
        echo $db->dbName;
}
else{
        echo "error";
}

and i am calling Userauthentication.php from index.php

here is the problem, when i  am running index.php , its saying :Call to a member function getDb() on a non-object in C:\Inetpub\wwwroot\Oppswapout\Admin\DBClass\UserAuthentication.php on line 28

this getDB() is running fine in default.php but when i put this GetDB() in UserAuthenticaton.php is giving  error( what i am guessing is, I am trying to call GetDb() from a class but GetDB() is an object of anotheer class , hence the trouble ??

Can you please explain me why and how to fix this issue ??

thanks in advance
'''''''''''''''''
default.php
''''''''''''''''
 
 
<?php
// Include the MySQL class
require_once('../DBClass/MySQL.php');
require_once ('../DBClass/ConnectionString.php');
require_once('../DBClass/MySQLResult.php');
require_once('../Functions/functions.php');
 if($db->getDb() != ""){        
        echo "Connected to the Server<br>";
        echo $db->dbName;
}
else{
        echo "error";
}
 ?>
 
####################### workable Code finish#######################
'''''''''''
UserAuthentication.php ( class )
''''''''''''''''''
<?php
 
class UserAuthentication 
{ 
 
 
function register()
        {
        if($db->getDb() != ""){ 
        echo "Connected to the Server<br>";
        echo $db->dbName;
}
else{
        echo "error";
}
 
        }
 
}
 
?>
############################### class definition finish################
'''''''''''''''
index.php ( calling the class)
'''''''''''''''''''''''''
<?php
 
require_once('../DBClass/MySQL.php');
require_once ('../DBClass/ConnectionString.php');
require_once('../DBClass/MySQLResult.php');
require_once('../DBClass/UserAuthentication.php');
 
$ObjUserAuthentication = new UserAuthentication();
 
 
    $ObjUserAuthentication->register();
 
?>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros