Using the same MySQL connection on different PHP pages

I am building a simple web application in PHP for my college project. I am using a MySQL database.

I am connecting to the database in login.php. After connecting, I will assign the connection $ _SESSION ["conn"], and then redirect to main.php.

In main.php I am writing $ conn = $ _SESSION ["conn"]. But the connection in $ conn does not work.

I thought that with the completion of the login.php script, the connection is closed. So I tried to use mysql_pconnect instead of mysql_connect, but this does not work either.

I know that I can connect to the database in every PHP file. But I do not want to do this. I want to use the same connection in all PHP files.

+4
source share
5 answers

Instead of saving the database connection in the 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, put your connection in db.php:

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

and then enter it in login.php:

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

Then you can do the same for each PHP file that needs access to the database, and you only have to change the credentials of access to the database in one file.

+9
source

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

You probably want to read PHP sessions . You cannot store resources (database connections, files, etc.) in a session because they cannot be serialized and saved.

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

Other answers are correct - you will need to connect to the database each time the script is called. Put the connection in a common include file for convenience.

+6
source

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 will need to connect again in this new process in order to run queries.

+2
source

What I usually have is a connection class that will require pages to establish a connection. Sort 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 connections on any pages.

0
source

It sounds like you want your php connection racks to be stable, and if so, then you need to create a PDO object with the required parameters to create a new PHP PDO object that will try to connect to mysql, and try passing the value in object initialization the persistancy parameter is true, you may have the same PDO Connection Objetc in every php script, but you will encounter such problems ... but it does not reconstruct like PHP Programming Best Pratices ... the best way to do this is to include a connection file for each script in in our project. Read the PHP documentation for persistent connections to learn more about the issues found for these connection objects, especially for the latest versions of php. PHP Announced that Persistant Connections are on the way to uninstalling from the futur version, as this may increase the load on the server with persistent resources ... Sorry if it's too late

0
source

Source: https://habr.com/ru/post/1301255/


All Articles