r/PHPhelp • u/ariakas3 • Jul 15 '25
Php db connection
Hi, i have been learning php and i am learning pdo class and mysql database. I am using xamp at the moment for local development. I have a question about db connection.
When i connect to a database with 'index.php' for ex and i use pdo class, does that connection end when i close the page? I've read that i have to connect to the db once, otherwise multiple connections can damage the database.
But as i understand, when i close the page, the connection dies. So is it ok to reconnect db everytime by opening index.php if i close it?
10
Upvotes
7
u/equilni Jul 15 '25
Based on u/colshrapnel reply, you may find some (bad) tutorials that tell you to close the connection - you don't need to do this.
Open the connection once and pass the created object where it needs to go to.
Pseudo code:
5
u/colshrapnel Jul 15 '25
Good question.
Yes.
Yes, per request.
It means, while your php is serving one request, the main script that received this request and all other other scripts that were included, must use a single db connection hence it must be just one connection in your code, used by all other scripts.
But since PHP very conveniently dies after processing a single request, it has to connect again, so when index (or any other script for that matter) is called again, it connects anew and then this new connection is used while this request being processed (to get the user's info, to get the data for displaying, to save some statistics, etc.)
4
u/Big-Dragonfly-3700 Jul 15 '25
The database connection is automatically closed by php when the code for any http request ends (all resources created for that request are destroyed.) This normally occurs long before you ever see anything rendered on a web page in a client/browser. When you have a web page open in a client, the web server is NOT sitting there waiting for anything to happen concerning that page. It has long since gone on to service other http requests. So yes, each new http request requires that you make a new database connection.
Each web page should make a single database connection, then use that connection throughout the rest of the code on that page. Multiple connections on a page won't damage anything, but consume processing time and memory resources, both on the web server and the database server, which slow down the operation of the site and make it easier for someone to perform a denial-of-service (DoS) attack on your site.
2
u/AshleyJSheridan Jul 16 '25
PHP is run on the server, and open file handles and DB connections are closed once all the content in a process has been sent down and that process has finished. You could keep the page open in your browser for days after, but it won't affect the connections because that part has already finished.
There are some exceptions, but generally, each request closes off open connections once that script has run its course on the server.
1
u/[deleted] Jul 16 '25
You already got the answer, but where did you get the idea that mysql can only have one connection to it at a time or it will "damage the database"? That's is absurd. Relational databases support many simultaneous connections and use various forms of locking to support "concurrency". Concurrency = multiple connections/users at the same time. My primary advice for someone who doesn't know much about MySQL/MariaDB or other forks, is that you should make sure all your tables are defined to use the InnoDB engine. MySQL/MariaDB is a little unusual in that it support different "engines" which work differently. The engine handles many aspects of how MySQL will actually handle locking. You can look up "database ACID Model" if you want to learn more about some of the features that InnoDB provides. With current versions of MySQL and MariaDB, InnoDB is now the default engine, but it never hurts to make sure your existing tables are using Innodb (you can alter them if they are not) and that any current and future table definition SQL statements specify it explicitly with the "engine" keyword.
0
u/excentive Jul 15 '25
As others have stated, its pretty self-healing.
Some side notes:
SHOW FULL PROCESSLISTon your mysql server to see whats going on.0
u/Guimedev Jul 17 '25
The PDO only connect when needed.
-2
u/[deleted] Jul 15 '25 edited Jul 15 '25
yes the connection ends if its not declared to close in the script when you either navigate away or close the page.
In the php manual somewhere it will say "connection is maintained for the lifetime of that object" If the object already is active it will not open another connection.
but you can only corrupt a mysql database table with concurrent updates on the same line. Which in multi user concurrent updates to a line in a table, you use table lock mechanism with transactions. which is appending FOR UPDATE at the end of the select query to lock the table so other selects from the software from other users will wait until the it transaction is done.
But that is particular with inode DB tables. That is why the lock tables option call was made.
as for @colshrapnel he needs to go to school and learn things before trying to comment on things it doesn't know.
-1
u/[deleted] Jul 16 '25
[removed] — view removed comment