Avatar of fosiul01
fosiul01Flag for United Kingdom of Great Britain and Northern Ireland asked on

Call to a member function on a non-object

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();
 
?>

Open in new window

PHP

Avatar of undefined
Last Comment
fosiul01

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
HuyBD

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
HuyBD

because of $db is global variable
ASKER
fosiul01

Hi yah
thanks

your solution worked, and also if i put "global $db"  in my connectionstring.php its works aswell
ASKER
fosiul01

Sorry its me again..

I put "global $db" in connectionSTring.php ( class)

and i remove global $db from userAuthentication.php ( class)

but it did not work, i thought it workd

if i declare a variable global to any class will i not be able to access this from another class ??
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes