BloodyMind Posted August 31, 2008 Share Posted August 31, 2008 can I make a function that returns an object ? coz I have made that using mysqliResult object and tried to pass it to a function but i get this error Call to a member function fetch_assoc() on a non-object Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 31, 2008 Share Posted August 31, 2008 You would need to post your code to get help with it. Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted August 31, 2008 Author Share Posted August 31, 2008 <?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 Quote Link to comment Share on other sites More sharing options...
knowj Posted August 31, 2008 Share Posted August 31, 2008 that usually means your SQL query is wrong (i get it 50+ times a day) you need to echo out your database error message once that's solved it should work fine. Quote Link to comment Share on other sites More sharing options...
ionik Posted September 1, 2008 Share Posted September 1, 2008 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) Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted September 1, 2008 Author Share Posted September 1, 2008 It's a simple select query as u see nothing is wrong with it and I didn't get u ionik actually I was trying to separate queries in a class so i can modify it easily later but i think i'll change this strategy now and do it as i used to Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted September 1, 2008 Author Share Posted September 1, 2008 I spotted the problem sorry for confusing you and thanx alot for trying to help it was just an = in an condition which made it non-object Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.