drui0508 2010-06-19 10:12
浏览 30
已采纳

PHP:如何确定数据库连接是否打开[重复]

This question already has an answer here:

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?

</div>
  • 写回答

6条回答 默认 最新

  • douyiavxxh02727 2010-06-19 10:19
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号