Lets say I’ve got a function that makes calls to PHP::PDO, the function opens a connection, runs the MySQL, closes the connection, returns some data.
I haven’t seen any serious problems with this, but is there any reason why this is a bad practice? What are the risks of doing this?
No risks that I’m aware of, but there is network lag for establishing connections. It will likely work fine now when you are just connecting to
localhost, but your application will probably have scaling issues if you’re establishing multiple connections for each HTTP request and you ever want to move to a secondary database server for whatever reason.A PHP page is inherently single-threaded, so unless you are starting up multiple threads yourself then there’s no (extra) concurrency issues. It’s still a good idea to use InnoDB and SQL transactions to help prevent concurrency issues across multiple requests.