Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase connection caching problem
    text
    copied!<p>I am using a script to cache statically DB and PDO statements executed.</p> <p>here it is</p> <pre><code> &lt;?php require_once 'ExceptionHandler.php'; final class Database { private static $db = "test"; private static $host = "localhost"; private static $username = "root"; private static $password = ""; private static $dbConn = null; private static $queryCatch = array(); private static function CONNECT() { if(self::$dbConn === null) { $connUrl = "mysql:host=".self::$host.";dbname=".self::$db; self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT =&gt; true)); } } public static function query($sql) { Database::CONNECT(); if(isset(self::$queryCatch[$sql]) &amp;&amp; is_object(self::$queryCatch[$sql])) { $query = self::$queryCatch[$sql]; } else { $query = self::$dbConn-&gt;prepare($sql); self::$queryCatch[$sql] = $query; } $numargs = func_num_args(); $arg_list = func_get_args(); //start from 1st parameter as 0th parameter is the query for ($i = 1; $i &lt; $numargs; $i++) { if(is_int($arg_list[$i])) { $query-&gt;bindParam($i,$arg_list[$i],PDO::PARAM_INT); } else { $query-&gt;bindParam($i,$arg_list[$i],PDO::PARAM_STR); } } $query-&gt;execute(); return $query; } } ?&gt; </code></pre> <p>I wanted to make this not just statically in page caching but a global $_SESSION caching</p> <p>But the following changed to my connect method is not helping</p> <pre><code>private static function CONNECT() { if(self::$dbConn === null) { if(isset($_SESSION['X_DB_CONN'])) { self::$dbConn = $_SESSION['X_DB_CONN'];echo "session cache hit"; } else { $connUrl = "mysql:host=".self::$host.";dbname=".self::$db; self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT =&gt; true)); $_SESSION['X_DB_CONN'] = self::$dbConn; if(isset($_SESSION['test'])) { echo ":)"; $_SESSION['test'] = "OO"; } echo "session cache NOT hit"; } } } </code></pre> <p>I have started the session properly. To Give proof and stating my problem:</p> <p>The $_SESSION['test'] is set from another page to ":)". And :) is the output from the if statement on this page.</p> <p>Beides always</p> <blockquote> <p>session cache NOT hit</p> </blockquote> <p>is displayed</p> <p>This is the output by the standard php error console</p> <blockquote> <p>Fatal error: Exception thrown without a stack frame in Unknown on line 0</p> </blockquote> <p>I have included the session_start() so that shouldn't be the problem</p>
 

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