tags:

views:

97

answers:

5

Hi, I have the following class method -

class Someclass
{
    /* Other properties and methods */

    public function close_connection($connection=NULL)
    {
        return mysql_close($connection)
    }
}

Now, before calling the mysql_close function in the above code, I want to check if the $connection points to a open database connection. That is, I want to be sure that the connection I am closing is open and has not already been closed.

How can I do that?

A: 

just use @mysql_close($connection) to suppress errors/warnings.

Imre L
You should note though, that the usage of the [Error Control Operator](http://de2.php.net/manual/en/language.operators.errorcontrol.php) is generally discouraged, as indicated by the warning on the manual page.
Gordon
Gordon: I working in a huge company, where's is "a-must" to turn off all errors in production. so this kind of tricks is ok.
holms
@holms: I disagree, do a check to prevent the error, don't just hide it. Anyways you can use `error_reporting` to hide errors in PHP rather than prepending @ to all the functions...
nico
@nico: the most humanistic way to handle errors is exceptions. although it's problematic to use humanistic ways in php at all, when you have to maintain a system which is a big piece of shit which costs 20k$ to create it.. that's a php reality.
holms
@holms I agree to turning off errors in production. But like @nico said this is better controlled globally from [`error_reporting`](http://de2.php.net/manual/en/function.error-reporting.php) and the [`display_errors`](http://de2.php.net/manual/en/errorfunc.configuration.php#ini.display-errors) directive. By the way, bad software is a reality in every language.
Gordon
Well its a closing statement. You dont care if it succeeds (genrerally), because the end result is the same. You want to keep `error_reporting` to something more useful.
Imre L
+2  A: 

If you have other code that could be closing the connection, have them set the connection to null so you can test for that.

If you are unsure wether the connection has been closed from the other end, due to a timeout or a network error, the only way to test it is to actually send data through using something like mysql_ping. However, this would be a terrible waste of resources if you are just trying to avoid closing a connection that might already be closed.

Note: as mentioned by evord, using mysql_ping will attempt to reopen the connection if it is no longer open.

kasperjj
Thank you very much......
Night Shade
+5  A: 

You could try mysql_thread_id($connection). That'll return false if there's no usable connection.

Evord
your answer is best! =) thnx
holms
This function basically does the same amount of work as mysql_ping. However, the ping function was actually written specifically for this purpose.
kasperjj
I think mysql_ping attempts to reopen the connection though.
Evord
Ah.. absolutely true!
kasperjj
A: 

I would use MYSQLI extension for this purpose, because this extension automatically handles all connections, and you don't need to worry but it at all. This extension is in every hosting =) For me it's a default for 4 years already =)

another way maybe is the best to handle exception for example, if dont want to use @mysql_close() then just catch exception and do nothing =) return true...

holms
+5  A: 

You could try checking if your $connection variable is infact a valid resource.

<?php
if(is_resource($connection))
{
    mysql_close($connection);
}
?>

Edit: Okay, this updated code now includes Gordon's suggestion.

<?php
if(is_resource($connection) && get_resource_type($connection) === 'mysql link')
{
    return mysql_close($connection);
}
else
{
    return false;
}
?>
Note that this would return `TRUE` even if `$connection` is not a [mysql link resource](http://de2.php.net/manual/en/language.types.resource.php). You could use [get_resource_type()](http://de2.php.net/manual/en/function.get-resource-type.php) to make sure.
Gordon