Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring Sessions in a DB in PHP - keep getting mysql error
    primarykey
    data
    text
    <p>I want to store all my sessions in a DB and have read up on this and implemented the following class: </p> <pre><code>&lt;?php /** * This class handles users sessions and stores the session in the DB rather than in a file. This way stops any * shared host security problems that could potentially happen. */ class sessionHandler { /** * Initial constructor which takes the database object as a param, to do all the database stuff * @param object $db The datase object */ public function __construct ($db) { $this-&gt;db = $db; $this-&gt;setHandler(); } function setHandler() { session_set_save_handler(array(&amp;$this, "open"), array(&amp;$this, "close"), array(&amp;$this, "read"), array(&amp;$this, "write"), array(&amp;$this, "destroy"), array(&amp;$this, "clean") ); } /** * Initiate a database object if necessary */ function open() { $this-&gt;db-&gt;connect(); } /** * Write session id and data to the database * @param string $id The hashed 32 char session id, unique to a user * @param string $data Serialized session array from the unique session * @return id The newly inserted ID of the database */ function write($id, $data) { $access = time(); $dateAdded = date("Y-m-d G:i:s"); $this-&gt;db-&gt;wrapper-&gt;where(array("sessionId"=&gt;$id)); $this-&gt;db-&gt;query($this-&gt;db-&gt;wrapper-&gt;delete(__CLASS__)); //fopen a file and store this in it that way we can debug $query = $this-&gt;db-&gt;wrapper-&gt;insert(__CLASS__, array("sessionId"=&gt;$id,"dateAdded"=&gt;$dateAdded,"sessionData"=&gt;$data)); $this-&gt;db-&gt;query($query); return $this-&gt;db-&gt;insertId(); } /** * Retrieve the session data for a given session id * @param string $id The hashed 32 char session id, unique to a user * @return string The session data found for the given session id */ function read($id) { $id = $this-&gt;db-&gt;wrapper-&gt;escape($id); $row = $this-&gt;db-&gt;fetch(1, $this-&gt;db-&gt;wrapper-&gt;get_where(__CLASS__,array("sessionId"=&gt;$id)), array(),false); if ($row) { return $row['data']; } return ""; } /** * Delete a session from the database by its unique session id * @param string $id The hashed 32 char session id, unique to a user * @return integer The number of deleted rows - should only ever be 1 */ function destroy($id) { $id = $this-&gt;db-&gt;wrapper-&gt;escape($id); $this-&gt;db-&gt;wrapper-&gt;where(array("sessionId"=&gt;$id)); $this-&gt;db-&gt;query($this-&gt;db-&gt;wrapper-&gt;delete(__CLASS__)); return $this-&gt;db-&gt;affectedRows(); } /** * Garage collector which deletes old records in the database, delete sessions that have expired. This is * determined by the session.gc_maxlifetime variable in the php.ini * @param integer $max The maximum number of seconds allowed before a session is to be considered expired * @return integer The number of deleted rows */ function clean($max) { $old = time() - $max; $old = $this-&gt;db-&gt;wrapper-&gt;escape($old); $this-&gt;db-&gt;wrapper-&gt;where(array("access"=&gt;$old), "&lt;"); $this-&gt;db-&gt;query($this-&gt;db-&gt;wrapper-&gt;delete(__CLASS__)); return $this-&gt;db-&gt;affectedRows(); } /** * Close the database connection once a read / write has been complete */ function close() { $this-&gt;db-&gt;close(); } /** * End the current session and store session data. */ public function __destruct(){ session_write_close(); } } </code></pre> <p>As you can see in my code, i pass the DB object into the class as a param. My bootstrap file is as follows: </p> <pre><code> $db = mogDB::init(); $sh = new sessionHandler($db); session_start(); </code></pre> <p>I have used a few of my own classes here so MogDB:init() basically creates a database connection with the right credentials. The wrapper stuff is basically so i dont have to type out sql query after sql query (me being a bit lazy i guess). </p> <p>But the problem i get is this in my php error log:</p> <pre><code>08-Apr-2010 17:40:31] PHP Warning: mysql_insert_id(): 11 is not a valid MySQL-Link resource in /library/mysql.php on line 69 </code></pre> <p>I have debugged this as much as i can and it seems that when it tries to write the session to the DB, it is failing. I have managed to save the query to a file and that imports fine into the database via phpmyadmin, so its not the query thats the problem.</p> <p>line 69 in mysql.php is as follows: </p> <pre><code>68. public function insertId() { 69. return mysql_insert_id($this-&gt;id); 70. } </code></pre> <p>Any help would be much appreciated Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload