Using same MySQL Connection in different PHP pages

前端 未结 5 1999
一生所求
一生所求 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:21

    The second request may not be served by the same web server process as the first, which means that you will have a completely separate set of database resources. You'll need to connect again in this new process in order to run queries.

    0 讨论(0)
  • 2021-01-19 04:28

    It sounds that you want to make your php connections persistants , in that way and if it is so , then you have to create a PDO Object with the required parameters to create a new PHP PDO Object that will attempt to connect to mysql , and in the object instanciation , try to pass persistancy option value to true , you may have available in every php scripts the same PDO Connection Objetc but you will end up to face issues with that way ... but it is not reconmmanded as PHP Programming Best Pratices ... the best way to do so is to include a connection file for every script in your project. Read PHP Documentation for Persistant Connections in order to learn more about Issues found for these Connection Objets especially for recent php versions. PHP Announced that Persistant Connections are on the way to be dropped from futur version as it may increase server workload with persistant ressources ... Sorry if it is too late

    0 讨论(0)
  • 2021-01-19 04:30

    What I normally have is a Connection class that pages will require in order to establish a connection. Something along the lines of:

    class Connection    {
        public $dbConnection = null;
        public $isConnectionActive = false;
        private $dbServer = null;
        private $dbCatalog = null;
        private $dbUser = null;
        private $dbPassword = null;
    }
    

    This class handles opening and closing of the connection on any pages.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-19 04:33

    Instead of saving the DB connection in a session you should make the connection calls in a separate file such as db.php and then require it from each of your scripts. For example, place your connection in db.php:

    mysql_connect('...', '...', '...');
    mysql_select_db('...');
    

    and then bring it in in login.php:

    require('db.php');
    $res = mysql_query('...');
    

    You can then do the same for each PHP file that needs access to the DB and you'll only ever have to change your DB access credentials in one file.

    0 讨论(0)
提交回复
热议问题