How to Check Whether mysqli connection is open bef

2020-05-23 02:43发布

问题:

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.

I tried

if($connection)
{
  mysqli_close($connection);
}

But it is not working. Any Solution?

回答1:

Old question, but may help someone else. 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);
?>


回答2:

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.



回答3:

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 {
  public $connected;

  public function __construct($host, $username, $password, $dbname) { // db info
    $conn = new mysqli($host, $username, $password, $dbname);

    if (!$conn->connect_errno) {
      $this->connected = true;
    }

    return $conn;
  }

  public function close($conn) {
    if ($this->connected) {
      $conn->close();
      $this->connected = false;
    }
  }
}

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



回答4:

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.



回答5:

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.";
}


回答6:

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.



标签: php mysqli