PHP:如何确定数据库连接是否打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3075116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:37:50  来源:igfitidea点击:

PHP: How to determine if a database connection is open

phpmysql

提问by MD Sayem Ahmed

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_closefunction in the above code, I want to check if the $connectionpoints 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.

现在,在调用mysql_close上面代码中的函数之前,我想检查是否$connection指向一个打开的数据库连接。也就是说,我想确保我正在关闭的连接是打开的并且尚未关闭。

How can I do that?

我怎样才能做到这一点?

采纳答案by kasperjj

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

如果您有其他可能关闭连接的代码,请让他们将连接设置为 null,以便您可以对其进行测试。

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.

如果您不确定连接是否已从另一端关闭,由于超时或网络错误,测试它的唯一方法是使用诸如 mysql_ping 之类的东西实际发送数据。但是,如果您只是想避免关闭可能已经关闭的连接,这将是一种可怕的资源浪费。

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

注意:正如 evord 所提到的,如果连接不再打开,使用 mysql_ping 将尝试重新打开连接。

回答by user353297

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

您可以尝试检查您的$connection变量是否实际上是有效资源。

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


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

编辑:好的,这个更新的代码现在包括Gordon的建议。

<?php
if(is_resource($connection) && get_resource_type($connection) === 'mysql link')
{
    return mysql_close($connection);
}
else
{
    return false;
}
?>

回答by Travis

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

你可以试试 mysql_thread_id($connection)。如果没有可用的连接,那将返回 false。

回答by acarito

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:

我找到了一个很好的解决方案,即结合 Travis 和 user353297(和 Gordon)的答案以及我自己的一些答案:

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

编辑:我发现我之前聚合和使用的解决方案实际上并没有正常工作。这是因为我使用的是 mysqli 而不是 mysql。我编辑的答案提供了先前代码的工作版本,但适用于 mysqli 连接对象(实际上,它不是资源)。

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

我在这里找到了解决方案:检查变量是否属于 mysqli 对象类型?

New answer (and working properly for mysqli):

新答案(并且适用于 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!

非常感谢 Travis、user353297 和 Gordon 给我提供的信息,我需要把这些信息放在一起!

回答by holms

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 =)

为此,我会使用 MYSQLI 扩展,因为该扩展会自动处理所有连接,您根本不需要担心。这个扩展在每个主机中都存在 =) 对我来说它已经是 4 年的默认设置 =)

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

另一种方式可能是处理异常的最佳方式,例如,如果不想使用@mysql_close() 那么只捕获异常并且什么都不做 =) 返回 true...

回答by Imre L

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

仅用于@mysql_close($connection)抑制错误/警告。