r/PHPhelp • • 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

21 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jul 25 '25

take for instance when someone makes a column that the last row is a vital part of the front end software. For example, this column is the end balance to a cash registrar drawer. So the insert sequence is vital or else the end user data would be inaccurate. Now think of other ledgers like store money stored in a safe, or a credit card transactions ledger. these are just simple example of the last row accuracy that is built upon previous rows and why the front end software that would be written to use lock tables so others wait until the last row is inserted.

So it really depends on what you are using the database for. So the concept of corrupt would be inaccurate data on the last row.

Now if you use it as a storage engine for something like Reddit or Facebook, then that wouldn't matter since that would be just a timestamp based recall.