Using same MySQL Connection in different PHP pages

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

提交回复
热议问题