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

Call to a member function on a non-object in

HI
Can i not call function  class function1 which is inside class1.class from  another function call funciton2 which is inside class2.class ??

if you can look at bellow code , its saying :Fatal error: Call to a member function query() on a non-object in.

Here I wrote a function( Register) to insert username and password in the table. This Register function is in UserAuthentication class.
problem is in :  query($sql); , which is actually a function from mysql.class

now i cant call this query($sql)  from Register function .

what i will have to do to call this function ??



'''''''''
UserAuthentication.class
''''''''''''''''''''
<?php
class UserAuthentication 
{ 
function register($username, $password, $UserEmail)
 	{
	
		// sql query to Select all username with the selected username provided by user
		$sql = "SELECT * FROM users where username = '$username'";
		$result = &$db->query($sql);  <<------------- problem---------------------
		
		Return true;
	
	}
 
}
 
?>
'''''''''''''''''
Register.php
''''''''''''''''''
<?php
require_once('../DBClass/MySQL.php');
require_once ('../DBClass/ConnectionString.php');
require_once('../DBClass/MySQLResult.php');
require_once('../DBClass/UserAuthentication.php');
$username = $_POST[Username];
$userpassword = $_POST[Userpassword1];
$userpassword2 = $_POST[Userpassword2];
$userEmail = $_POST[UserEmail];
// Creating object of UserAuthentication class
$ObjUserAuthentication = new UserAuthentication();
 
   $ObjUserAuthentication->register($username,$userpassword,$userEmail);
   ?>
  } 
?>

Open in new window

PHP

Avatar of undefined
Last Comment
fosiul01

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
- -

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
ASKER
fosiul01

ommm if i do that, then it giving Error in Mysql class which is in mysql.php

mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\Inetpub\wwwroot\MySql.php on line 138
- -

Could you please post here the file in which the $db is defined?
Thanks.
SOLUTION
lionheart620

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
ASKER
fosiul01

Hi yah, sorry, due to  night i was unable to reply back.

I have attached a workable code, check this out , see how my class is defined and wh at is the output, it might give you some clue.
if i run workable code, the out put would be : Connected to the Server
                                                                           Table Name


Now see the problematic class and page.

the difference is, i put bellow code in the seperate class

if($db->getDb() != ""){      
      echo "Connected to the Server<br>";
      echo $db->dbName;

and Fatal error: Call to a member function getDb() on a non-object in


so where is the problem ??
'''''''''''''''''
Workable Code
''''''''''''''''
 
 
<?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################
'''''''''''''''
webpage ( 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

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