Click to See Complete Forum and Search --> : mysql connection close issue


tkm
05-14-2008, 05:43 AM
Hello Mates,
I think I am not being able to close mysql connection properly from php. Here is what I am doing:

mysql_connect($connection);

function x()
{
global $connection;
mysql_select_db($db_name,$connection);
...code...code...code...
mysql_close($connection);
}
function y()
{
global $connection;
mysql_select_db($db_name,$connection);
...code...code...code...
mysql_close($connection);
}

Is my approach above ok? Or am I doing crap?

Any suggestion would be great help. Thank you.

NogDog
05-14-2008, 02:51 PM
I would recommend passing parameters to your functions rather than using global variables (which result in tightly coupled code).

function x($connection, $db_name)
{
mysql_select_db($db_name, $connection);
// ...etc....
}

Also, depending on what you are doing, you may not want to do the mysql_close() within those functions, as it means that once you call one of those functions, the connection will be closed, so you'll have to do another connect before calling either of them again.