<b>Fatal errCall to a member function query() on a non-objec

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

<b>Fatal errCall to a member function query() on a non-objec

Post by psychotomus »

How do I workaround this? says error is line 42

here is the begining of my file. The rest is just functions. So i'll show the function that is causing it.

Code: Select all

<?
//session stuff
session_start();

//include required files.
include "../config.php";
include "../phpMySQL.php";
include "../includes/constants.php";

//initiate class.
$db = new phpMySQL($dbhost, $dbuser, $dbpass, $dbname);
$db->connect();

line 42 is $query = $db->query

Code: Select all

//attempt to login user
function userLogin()
{
	//check if username pass string test
	if(checkUser() == true && checkPass() == true)
	{
		//query to check if user name or email in use
		$query = $db->query("SELECT userID, userActCode, userRank, COUNT(userID) as counts FROM " . USER_TABLE . "' WHERE userHandle='" . $db->escape($_POST['textUser']) . "' OR userPass='" . $db->escape($_POST['textPass']) . "'");
		$rec = $db->fetch_array($query);
		
		//user name and password match
		if($rec['counts'] == 1)
		{
			//user account activated
			if($rec['userActCode'] == 0)
			{
				$_SESSION['userID'] = $rec['userID'];
				$_SESSION['userRank'] = $rec['userRank'];
				
				//update last activitity
				$data = array("userLastActive" => time());
				$db->update(USER_TABLE, $data, "userID='" . $rec['userID'] . "'");
				
				die('Login Successfull, Redirecting...<script type="text/javascript">window.location = "' . $pather . '"</script>');
			}
			else
			{
				//display error
				die("You must first activate your account...");
			}
		}
		else
		{
			//display error
			die("Incorrect User name and password combination...");
		}
	}
	else
	{
		//display error
		die($_SESSION['msg']);
	}
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: <b>Fatal errCall to a member function query() on a non-o

Post by social_experiment »

Is the first section of code on the same page as your second piece? Try creating an instance of the object inside the function.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: <b>Fatal errCall to a member function query() on a non-o

Post by John Cartwright »

social_experiment wrote:Is the first section of code on the same page as your second piece? Try creating an instance of the object inside the function.
Better yet, pass the $db instance as a parameter to the function.

Code: Select all

function userLogin($db) 
{
    $db-> ...
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: <b>Fatal errCall to a member function query() on a non-o

Post by social_experiment »

:) Yeah that IS a better way to do it.
EDIT
Removed ambiguity.
Last edited by social_experiment on Sat Feb 05, 2011 12:51 am, edited 1 time in total.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: <b>Fatal errCall to a member function query() on a non-o

Post by John Cartwright »

social_experiment wrote::) Yeah that's probably a better way to do it
It IS a better way to do it for the following reasons

1) You don't open multiple connections + don't have multiple instance floating around in memory
2) Established an interface for dependency injection. Now, the function can accept any $db object instead of the method having it hard coded. This is important when unit testing and re-factoring (when requirements change).
Post Reply