How to Check Whether mysqli connection is open before closing it

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

    This is from PHP.net.

    Object oriented style


    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
    
    /* check if server is alive */
    if ($mysqli->ping()) {
        printf ("Our connection is ok!\n");
    } else {
        printf ("Error: %s\n", $mysqli->error);
    }
    
    /* close connection */
    $mysqli->close();
    ?>
    

    Procedural style


    <?php
    $link = mysqli_connect("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    /* check if server is alive */
    if (mysqli_ping($link)) {
        printf ("Our connection is ok!\n");
    } else {
        printf ("Error: %s\n", mysqli_error($link));
    }
    
    /* close connection */
    mysqli_close($link);
    ?>
    
    0 讨论(0)
  • 2021-02-04 01:13

    Just don't close it. That would be the best solution ever.

    99.9% of time it's perfectly ok to leave the connection alone, PHP will close it for you.

    Only if your script has to perform some heavy time consuming task that doesn't involve a database interaction, you may want to close the connection before starting this operation. But in this case you will have the connection deliberately open as there will be no random closures scattered around the code and therefore will be no need to check whether it is closed already. Hence, just close it, in this particular but extremely rare case.

    All other time just leave it alone.

    0 讨论(0)
  • 2021-02-04 01:21

    Try this:

    function close_connection(){
        $thread = $mysqli->thread_id;
        $mysqli->close();
        $mysqli->kill($thread);
    
    }
    

    That will close the connection you opened using object oriented style like this:

    $mysqli = new mysqli($db_server, $db_username, $db_password, $db_name);
    

    That's the basic idea, but if you're using the procedural style, I'm sure you'll be able to custom the code as you need.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 01:23

    If you open a connection, it will stay open until it's explicitly closed or the script ends (unless persistent connections is on). Using the code you have should work.

    One option is to extend the mysqli class and have a status property called $connected:

    class CustomMysqli extends mysqli
    {
        protected bool $connected;
    
        public function __construct($host, $username, $password, $database)
        {
            parent::__construct($host, $username, $password, $database);
            $this->connected = ($this->connect_errno === 0);
        }
    
        public function close(): void
        {
            if ($this->connected) {
                parent::close();
                $this->connected = false;
            }
        }
    
        public function isConnected(): bool
        {
            return $this->connected;
        }
    }
    

    Checking for the $connected property is a bit overkill, but will ensure the connection is still open.

    0 讨论(0)
  • 2021-02-04 01:35

    Check connection errors. mysqli_connect() always returns a MySQLi object.

    use this:

    $mysqli_connection = new MySQLi('localhost', 'user', 'pass', 'db');
    if ($mysqli_connection->connect_error) {
       echo "Not connected, error: " . $mysqli_connection->connect_error;
    }
    else
    {
       echo "Connected.";
    }
    
    0 讨论(0)
提交回复
热议问题