Jump to content

Returning an object


BloodyMind

Recommended Posts

<?php

public function getUserLogin($in_username){
	$this->query = <<<EOQUERY
SELECT * FROM users WHERE username = '$in_username';
EOQUERY;
	$result = $this->db->query($this->query);
	try {
		if($result == FALSE){
			throw new Exception('Error:Cannot execute check query');
		}

	}catch (Exception $e){
		echo $e->getMessage();
	}
	if (is_object($result) == false) {
		echo "not object";
	}
	return $result;
}


// here is where the function call gets the error

private function confirmUserNamePasswd($in_user_name, $in_user_password,$in_db_conn = NULL){

	/*
	- internal arg checking
	- get a connection
	- get the record for the username.
	- verify the password
	*/
	if ($in_db_conn == NULL) {
		$conn = $this->getConnection();
	}else {
		$conn = $in_db_conn;
	}


	try {


		// 2. make sure incoming username is safe for queries.
		$user_name = $this->super_escape_string($in_user_name,$conn);

		// 3. get the record with this username

		if (!is_object($DA)) {
    		$DA = new usersDataAccess();
    	        }
    	
    	        // here Supposed to return the object

		$results = $DA->getUserLogin($user_name);

		if ($results = FALSE) {
			throw new DatabaseErrorException($conn->Error);
		}
		// 4. re-confirm the name and the passwords match
		$login_ok = FALSE;

		// here when i try to use the passed $results I get the error

		while ($row = $results->fetch_assoc() !== NULL) {
			if (strcasecmp($row['username'],$user_name) == 0) {
				// good, name matched.  does password?
				if (strcasecmp(md5($in_user_password),$row['password']) == 0) {
					$login_ok = TRUE;
					$user_id = $row['user_id'];
				}else {
					$login_ok = FALSE;
				}
				break;
			}
		}

		$results->close();

	}catch (Exception $e){
		if (isset($conn) and $in_db_conn === NULL) {
			$conn->close();
		}
		throw $e;
	}

	// only clean up what we allocated
	// if there wasn't a db con we close it to clean
	if ($in_db_conn === NULL) {
		$conn->close();
	}
	// throw on failure, or return the user ID on success
	if ($login_ok == FALSE) {
		throw new Exception('Invalid username/password');
	}
	return $user_id;

}



 

The Error

 

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\computek\bizlogic\userManager.php on line 334
Link to comment
Share on other sites

to have a function return a instance of a class object you would need to have that function be a reference to that object you are trying to call.

 

 

And that error is because you are trying to call fetch_assoc on $result which appears to be a reference ID?

 

 

for example

 

class Test
{
     public function __construct()
    {
        // Do something
    }

    public function thisis()
    {
        // Do something
    }

    public function thisthat()
    {
        return $this->thisis(); 
    }
}

 

Now of course that would be completely useless but adding in variables to the function and maybe some actions that happen that affect the thisis operation in the thisthat function.

 

It appears what you are trying to do would return the SQL reference to that query and not a object, I'm not sure on how your Database class operates but to me it appears your code should be this, does your DB class use caching for the last query since the refernce ID is not included for in the fetch_assoc.

 

while ($row = $this->db->fetch_assoc() !== NULL)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.