Page 1 of 1

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

Posted: Fri Feb 04, 2011 7:44 am
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']);
	}
}

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

Posted: Fri Feb 04, 2011 10:03 am
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.

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

Posted: Fri Feb 04, 2011 10:28 am
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-> ...
}

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

Posted: Fri Feb 04, 2011 3:44 pm
by social_experiment
:) Yeah that IS a better way to do it.
EDIT
Removed ambiguity.

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

Posted: Fri Feb 04, 2011 4:49 pm
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).