Hello, I am trying to transfer a database connection between two pages. The connection is passed to the controller through the session variable. The connection is functioning successfully until the user refreshes the page a second time. After the second page refresh, the static variable $instance starts to return NULL. How to save its value, regardless of the number of page reloads?

 <?php class Controller_upload extends Controller { private static $instance = null; function __construct() { $this->model = new Model_Upload(); $this->view = new View(); self::$instance=$_SESSION['dbase_conn']; } public static function getInstance() { if (null === self::$instance) { self::$instance = new self(); } return self::$instance; } private function __clone() {} function action_index() { if(isset($_SESSION['dbase_conn'])) { unset($_SESSION['dbase_conn']); } $data=self::$instance; $this->view->generate('upload_view.php', 'upload_template_view.php',$data); } } ?> 
  • The time machine moved us to the middle of the 200s :) - Dmitry Gvozd

3 answers 3

Only serializable values ​​can be passed through the session, the connection to the DBMS in standard extensions is a non-serializable resource type and cannot be transmitted via the session or otherwise between two requests. There is the concept of a persistent connection shared between several handlers, but there is definitely no binding to the session.

    The connection to the database is always broken when the PHP script ends.

    Connection with the database must be installed at the beginning of the PHP script.

    In the session, it is worth saving only what belongs to the user and should be saved between user requests.

    And some kind of crazy code: $_SESSION['dbase_conn'] and new self() written to $ instance.

      No Here even pconnect will not help you. Revise the logic of your algorithm.

      • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky