How to Check Whether mysqli connection is open before closing it

后端 未结 7 1842
野趣味
野趣味 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:36

    Got to check if there is a connection before ping

    CAUTION!!!

    ALWAYS do your job and explicity close your connection because other things may go wrong before php closes it by script ending

    <?php
    
    $link = mysqli_connect("localhost", "username", "pwd", "mydatabase");
    
    if (
    
      $link                 // has a connection ?
    
      && mysqli_ping($link) // is it connected ?
    
    ) {
    
    mysqli_close($link)
    
    }
    
    ?>

    0 讨论(0)
提交回复
热议问题