How to Check Whether mysqli connection is open before closing it

后端 未结 7 1843
野趣味
野趣味 2021-02-04 00:30

I am going to use mysqli_close($connection) to close the $connection. But Before Closing I need to ensure that the concerned connection is open.

<
7条回答
  •  再見小時候
    2021-02-04 01:23

    While some suggest to check for $connection->ping(),$connection->stat() or mysqli_thread_id($connection), the three will throw: 'Couldn't fetch mysqli' if the connection was closed before.

    The solution that worked for me was:

    if(is_resource($connection) && get_resource_type($connection)==='mysql link'){
        $connection->close(); //Object oriented style
        //mysqli_close($connection); //Procedural style 
    }
    

    PS: Checking only if it's a resource could be enough in a controlled context.

提交回复
热议问题