如何在关闭之前检查mysqli连接是否打开

时间:2021-03-04 14:07:59

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.

我将使用mysqli_close($ connection)来关闭$ connection。但在结束之前,我需要确保相关的连接是开放的。

I tried

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

But it is not working. Any Solution?

但它没有用。任何方案?

6 个解决方案

#1


24  

Old question, but may help someone else. This is from PHP.net.

老问题,但可能会帮助别人。这是来自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


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:

一种选择是扩展mysqli类并拥有一个名为$ connected的status属性:

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.

检查$ connected属性有点矫枉过正,但会确保连接仍然打开。

#3


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.

虽然有人建议检查$ connection-> ping(),$ connection-> stat()或mysqli_thread_id($ connection),但是如果之前连接已关闭,三者将抛出:'无法获取mysqli'。

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.

PS:仅在受控环境中检查资源是否足够。

#4


0  

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.

这是基本的想法,但如果您使用的是程序样式,我相信您将能够根据需要自定义代码。

#5


0  

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

检查连接错误。 mysqli_connect()总是返回一个MySQLi对象。

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


-3  

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

只是不要关闭它。那将是最好的解决方案。

#1


24  

Old question, but may help someone else. This is from PHP.net.

老问题,但可能会帮助别人。这是来自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


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:

一种选择是扩展mysqli类并拥有一个名为$ connected的status属性:

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.

检查$ connected属性有点矫枉过正,但会确保连接仍然打开。

#3


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.

虽然有人建议检查$ connection-> ping(),$ connection-> stat()或mysqli_thread_id($ connection),但是如果之前连接已关闭,三者将抛出:'无法获取mysqli'。

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.

PS:仅在受控环境中检查资源是否足够。

#4


0  

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.

这是基本的想法,但如果您使用的是程序样式,我相信您将能够根据需要自定义代码。

#5


0  

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

检查连接错误。 mysqli_connect()总是返回一个MySQLi对象。

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


-3  

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

只是不要关闭它。那将是最好的解决方案。