9

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?

0

6 Answers 6

21

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;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Note that this would return TRUE even if $connection is not a mysql link resource. You could use get_resource_type() to make sure.
This should be the accepted answer...
11

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

3 Comments

This function basically does the same amount of work as mysql_ping. However, the ping function was actually written specifically for this purpose.
I think mysql_ping attempts to reopen the connection though.
mysqli_thread_id(): Couldn't fetch mysqli
4

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.

Comments

1

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

6 Comments

You should note though, that the usage of the Error Control Operator is generally discouraged, as indicated by the warning on the manual page.
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: 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: 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 I agree to turning off errors in production. But like @nico said this is better controlled globally from error_reporting and the display_errors directive. By the way, bad software is a reality in every language.
|
1

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...

Comments

1

I found a good solution to be a combination of Travis and user353297's (and Gordon's) answers with a little addition of my own:

Old answer (incorrect):

if(is_resource($connection) && get_resource_type($connection) === 'mysql link')
{
    if($mysqli_connection_thread = mysqli_thread_id($connection))
    {
        $connection->kill($mysqli_connection_thread);
    }
    $connection->close();
}

Edit: I found that the solution I aggregated and used before wasn't actually working properly. This was because I am using mysqli not mysql. My edited answer provides a working version of the previous code but works with the mysqli connection object (which is not, in fact, a resource).

I found the solution here: check if a variable is of type mysqli object?

New answer (and working properly for mysqli):

if(is_object($mysqli_connection) && get_class($mysqli_connection) == 'mysqli')
{
    if($mysqli_connection_thread = mysqli_thread_id($connection))
    {
        $mysqli_connection->kill($mysqli_connection_thread);
    }
    $mysqli_connection->close();
}

I have created a function containing a few of these statements specific to certain database connections and I call the function at the end of every script once I am sure that I will not be using any of the connections anymore. This makes sure that the threads die, the connection closes - if it is open - and that the resources are freed up, which can become an issue if there are lots of connections to multiple DBs per user, as is my case.

Thanks a lot to Travis, user353297 and Gordon for giving me the info I needed to put this together!

1 Comment

In the new answer, if the connection was closed before, it throws: mysqli_thread_id(): Couldn't fetch mysqli.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.