sam69 Posted February 15, 2009 Share Posted February 15, 2009 Hey guys, I keep getting an error when calling a database (object) property in an object. I am storing a database class (extends mysqli) into session, passing that session into an object, accountlog, which works fine at the point of being instantiated. But on another page, when I call that accountlog object, and try to use the database variable, it craps out saying it "can't fetch Database". Could you please provide me with some insight as to what's happening? Here is some sample code: // Database class class Database extends mysqli { public function GetTimestamp(){ return date("Y-m-d H:i:s"); } /* public function __construct($Server, $User, $PW, $DB){ parent::__construct($Server, $User, $PW, $DB); if (mysqli_connect_errno() != 0) { throw new Exception(mysqli_connect_error(), mysqli_connect_errno()); } } */ public function SafeQuery(){ $arg_list = func_get_args(); foreach ($arg_list as $key => $value){ if ($key <> "0"){ $arg_list[$key] = $this->real_escape_string($value); } else { $arg_list[$key] = $value; } } $query = call_user_func_array("sprintf", $arg_list); try { $this->real_query($query); if ($this->errno!=0){ echo $this->error; throw new Exception($this->error, $this->errno); } if ($this->field_count){ $results = $this->store_result(); return $results; } } catch (Exception $ex){ return $ex; } } public function GetRow($Table, $Title, $ID){ if (get_magic_quotes_gpc()) { $Title = stripslashes($Title); $ID = stripslashes($ID); } $query = sprintf("SELECT * FROM $Table WHERE %s = '%s'", $this->real_escape_string($Title), $this->real_escape_string($ID)); $this->real_query($query); if (mysqli_error($this)) { throw new Exception(mysqli_error($this), mysqli_errno($this)); } if ($this->field_count){ $results = $this->store_result(); if ($results->num_rows > 0) { return $results->fetch_assoc(); } } return null; } } // AccountLog class AccountLog { private $_ID; private $_AccountID; private $_Activation; private $_Deactivation; private $_DB; private $_Account; public function __construct($Account, &$_DB){ $this->_Account = $Account; $this->_DB = $_DB; } ... } // Processing Page $_SESSION["_GLDB"] = new Database("myAwesomeServer", "blah", "blahblahPW", "myDatabaseName"); $_SESSION["AccountLog"] = new AccountLog(null, $_SESSION["_GLDB"]); // Index Page die($_SESSION["AccountLog"]->GetDB()->real_escape_string("aaaa")); // ERROR MESSAGE: Warning: mysqli::real_escape_string() [mysqli.real-escape-string]: Couldn't fetch Database in (etc) Quote Link to comment https://forums.phpfreaks.com/topic/145314-cannot-fetch-database/ Share on other sites More sharing options...
Mchl Posted February 15, 2009 Share Posted February 15, 2009 You can't store resources in session, and mysqli object contains a resource to database connection. http://static.zend.com/topics/0200-T-WP-1107-R1-EN-PHP-is-not-Java-Seesions-in-PHP.pdf Quote Link to comment https://forums.phpfreaks.com/topic/145314-cannot-fetch-database/#findComment-762874 Share on other sites More sharing options...
corbin Posted February 15, 2009 Share Posted February 15, 2009 It doesn't make much sense to store a Database instance in a session..... I usually tend to keep only data related to that specific user in sessions, although nothing is wrong with straying from that (personal habit). If you're trying to avoid the cost of reinitializing the class by storing it in a session, the cost will still be there (for the most part). If you really want to do it though, you can use __sleep() and __wakeup to kill and remake the DB connection (well, it would automatically be killed, so you wouldn't really need a sleep() method, unless you want to be correct and unset the resources). Quote Link to comment https://forums.phpfreaks.com/topic/145314-cannot-fetch-database/#findComment-762894 Share on other sites More sharing options...
sam69 Posted February 15, 2009 Author Share Posted February 15, 2009 It doesn't make much sense to store a Database instance in a session..... I usually tend to keep only data related to that specific user in sessions, although nothing is wrong with straying from that (personal habit). Doesn't it make sense to store the database instance though? It's a "black box" that takes care of all the DB work that needs to be done... So, in my AccountLog class, it doesn't need to know how to do any DB work, it just calls the instance and the rest is taken care of... If I have to keep reinitializing the DB connection on every page load, then I'll always have to re-pass it into any class that uses it... There must be a better way to do this process... Quote Link to comment https://forums.phpfreaks.com/topic/145314-cannot-fetch-database/#findComment-762957 Share on other sites More sharing options...
allworknoplay Posted February 15, 2009 Share Posted February 15, 2009 Do you have to serialize the object first before putting it into the DB? Quote Link to comment https://forums.phpfreaks.com/topic/145314-cannot-fetch-database/#findComment-762960 Share on other sites More sharing options...
Mchl Posted February 15, 2009 Share Posted February 15, 2009 I don't know any database system, that would have datatype called 'PHP object'... so yeah, you do. Quote Link to comment https://forums.phpfreaks.com/topic/145314-cannot-fetch-database/#findComment-762963 Share on other sites More sharing options...
sam69 Posted February 15, 2009 Author Share Posted February 15, 2009 I'm not passing an object to the database. I'm doing stuff like this: 1. Load Database instance 2. Load AccountLog instance (stores what pages the currently logged in user is viewing) 3. When DB and AccountLog are loaded, pass DB instance into AccountLog. 4. AccountLog will need to store the page the user is viewing- call a method (SafeQuery) from DB variable, which takes a SQL command with some parameters (similar to a stored proc in MSSQL). 5. AccountLog needs to save when the user logs off, call SafeQuery() in DB variable with appropriate command. The issue is, I have to reload the DB connection, and reattach that newly made connection into all the objects that use it (like AccountLog). Quote Link to comment https://forums.phpfreaks.com/topic/145314-cannot-fetch-database/#findComment-762966 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.