PDA

Click to See Complete Forum and Search --> : [RESOLVED] Is this database connection handling ok?


HoraShadow
Jul 28th, 2006, 03:57 AM
Hello guys,

I inherited a PHP application. In the db layer of the application, it has this function to connect to a mySql db.


function getConn() {
return mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
}


This function gets called at the beginning of every db function in the dblayer class.

Beeing new to PHP, I do not know if this is handled well. There's no connection closing on PHP? or it's handled automatically by a connection pool that times out?

Thanks
HoraShadow

CornedBee
Jul 28th, 2006, 04:31 AM
Basically, all db connections are closed when the script terminates.
Also, mysql_connect() and the other DB connections tend to cache handles and return the same handle for the same request.

However, personally I think you should still keep the connection handle in the class and only call getConn() once.
(I also think you should use the MDB2 DB abstraction layer if you're using PHP4, and the DBO extension if you're using PHP5, but that's a different story.)

Matt_T_hat
Jul 29th, 2006, 12:02 PM
Most code I know makes the connection at the start calls "actions" and closes leaning on the fact that you will mostly only want to talk tot he one database and therefore can get on with called mysql_query.