Fatal error: Call to a member function prepare() on a non-object (MySQL/Sessions)
<p>I'm aware that this is a common question on here, but I've been through a whole lot of other answers, and cannot understand why this is still failing. So, my apologies, but hopefully someone can point out where this is going wrong!</p>

<p>In <strong>index.php</strong>, the following code is executed:</p>

<pre><code>// Set Database Configuration
$dbconfig = array( 
    'host' =&gt; getenv('MYSQL_DB_HOST'),
    'user' =&gt; getenv('MYSQL_USERNAME'), 
    'pass' =&gt; getenv('MYSQL_PASSWORD'), 
    'name' =&gt; getenv('MYSQL_DB_NAME') 
); 

// Start database sessions
$db = oadb::getInstance();
$db-&gt;show_errors();
echo $db-&gt;prepare("hello");

// Open a new session
$sess = new oasession();
session_start();
</code></pre>

<p>The <strong>getInstance();</strong> and <strong>$db->prepare()</strong> happen without a problem, and the "hello" text is echoed to confirm this.</p>

<p>When the <strong>new oasession()</strong> is called is where the wheels start to fall off. The <strong>oasessions()</strong> function is a session handler that takes over PHPs normal session handling, and puts it in a MySQL database.</p>

<p>The <strong>sessions.inc.php</strong> includes a class as follows (I've removed some irrelevant functions):</p>

<pre><code>class oasession {

   var $life_time;

   function oasession() {

      // Read the maxlifetime setting from PHP
      $this-&gt;life_time = get_cfg_var("session.gc_maxlifetime");

      // Register this object as the session handler
      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, "gc" )
      );

   }

   function open( $save_path, $session_name ) {
        global $sess_save_path;
        $sess_save_path = $save_path;
        // Don't need to do anything. Just return TRUE.
        return true;
   }

   function read( $id ) {
       global $db;

           // Set empty result
           $data = '';

           // Fetch session data from the selected database

           $time = time();

           $newid = $db-&gt;prepare($id);
           $sql = "SELECT `session_data` FROM `OA-Auth_sessions` WHERE `session_id` = '$newid' AND `expires` &gt; $time";

           $rs = $db-&gt;query($sql);                           
           $a = $db-&gt;num_rows($rs);

           if($a &gt; 0) {
             $row = $db-&gt;fetch_array($rs);
             $data = $row['session_data'];
           }
           return $data;
        }

    function write( $id, $data ) {
        global $db;

         // Build query                
         $time = time() + $this-&gt;life_time;

         $newid = $db-&gt;prepare($id);
         $newdata = $db-&gt;prepare($data);

         $sql = "REPLACE `OA-Auth_sessions` (`session_id`,`session_data`,`expires`) VALUES('$newid', '$newdata', $time)";

         $rs = $db-&gt;query($sql);

         return TRUE;

      }
}
</code></pre>

<p>It is able to use the read($id) function without any problems, but when it tries to use the write($id, $data) function, it fails with the following error...</p>

<pre><code>Fatal error: Call to a member function prepare() on a non-object in sessions.class.php on line 66
</code></pre>

<p>...suggesting that the prepare function, executed from $db->prepare() is not a valid object.</p>

<p>I've tried testing this by placing print_r()'s before and after the new oasession(), and both have printed with object descriptions, and there are no problems running other $db->prepare() calls before or after the new oasession() call.</p>

<p>Any thoughts on what may be causing this one?</p>

<p>Thanks very much!</p>
