> For the complete documentation index, see [llms.txt](https://learnsql.gitbook.io/lernmysql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learnsql.gitbook.io/lernmysql/cach-kiem-tra-xem-ket-noi-mysqli-co-mo-hay-khong-truoc-khi-dong-ok.md).

# Cách kiểm tra xem kết nối mysqli có mở hay không trước khi đóng (ok)

Theo w3school Kiểm tra xem một biến có phải là tài nguyên hay không:

<https://www.w3schools.com/php/func_var_is_resource.asp>

Tôi sẽ sử dụng `mysqli_close($connection)` để đóng `$connection`. Nhưng trước khi đóng tôi cần đảm bảo rằng kết nối liên quan được mở.

Tôi đã thử

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

Nhưng nó không hoạt động. Bất kỳ giải pháp?[php](https://www.it-swarm-vi.com/vi/php/)[mysqli](https://www.it-swarm-vi.com/vi/mysqli/) 161 thg 5, 2013[Munib](https://stackoverflow.com/users/2207240/munib)

Câu hỏi cũ, nhưng có thể giúp đỡ người khác. Đây là từ [PHP.net](https://php.net/manual/en/mysqli.ping.php) .

### Phong cách hướng đối tượng

```
<?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();
?>
```

### Phong cách thủ tục

```
<?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);
?>
```

&#x20;274 thg 10, 2013[user2060451](https://stackoverflow.com/users/2060451/user2060451)

Trong khi một số đề nghị kiểm tra `$connection->ping()`, `$connection->stat()` hoặc `mysqli_thread_id($connection)`, ba người sẽ ném: '`Couldn't fetch mysqli` 'nếu kết nối bị đóng trước đó.

Giải pháp hiệu quả với tôi là:

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

PS: Chỉ kiểm tra nếu đó là [tài nguyên](http://de2.php.net/manual/en/language.types.resource.php) có thể đủ trong bối cảnh được kiểm soát. 416 thg 1, 2017[Leopoldo Sanczyk](https://stackoverflow.com/users/862270/leopoldo-sanczyk)

Nếu bạn mở một kết nối, nó sẽ vẫn mở cho đến khi nó đóng rõ ràng hoặc tập lệnh kết thúc (trừ khi kết nối liên tục được bật). Sử dụng mã bạn có nên làm việc.

Một tùy chọn là mở rộng lớp `mysqli` và có thuộc tính trạng thái gọi là `$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;
    }
  }
}
```

Đang kiểm tra `$connected` thuộc tính hơi quá mức, nhưng sẽ đảm bảo kết nối vẫn mở. 31 thg 5, 2013[Chris Bornhoft](https://stackoverflow.com/users/617036/chris-bornhoft)

Kiểm tra lỗi kết nối. mysqli\_connect () luôn trả về một đối tượng MySQLi.

dùng cái này:

```
$mysqli_connection = new MySQLi('localhost', 'user', 'pass', 'db');
if ($mysqli_connection->connect_error) {
   echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else
{
   echo "Connected.";
}
```

&#x20;116 thg 11, 2014[hamid](https://stackoverflow.com/users/1295141/hamid)

Thử đi:

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

}
```

Điều đó sẽ đóng kết nối bạn đã mở bằng cách sử dụng kiểu hướng đối tượng như thế này:

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

Đó là ý tưởng cơ bản, nhưng nếu bạn đang sử dụng kiểu thủ tục, tôi chắc chắn bạn sẽ có thể tùy chỉnh mã khi bạn cần.
