Using same MySQL Connection in different PHP pages

前端 未结 5 2005
一生所求
一生所求 2021-01-19 03:46

I am creating a simple Web Application in PHP for my college project. I am using the MySQL database.

I connect to the database in login.php. After connection I assig

5条回答
  •  走了就别回头了
    2021-01-19 04:32

    After connection I assign the connection to $_SESSION["conn"] and then redirect to main.php.

    You'll probably want to read up on PHP sessions. You can't store resources (database connections, file handles, etc) in a session, because they can not be serialized and stored.

    Keep in mind that each and every visit to a PHP script invokes a new instance of the PHP interpreter (via CGI, via FastCGI, or via a built-in module), and invokes a new instance of the script. Nothing is shared between script calls, because the entire environment goes away when the script exits.

    The other answers are correct -- you'll need to connect to the database on every script call. Place the connection in a common include file for convenience.

提交回复
热议问题