PHP mysqli_close() 函式



定義和用法

mysqli_close() 函式接受一個 MySQL 函式物件(先前已開啟)作為引數,並將其關閉。

您不能使用此函式關閉持久連線

語法

mysqli_close($con);

引數

序號 引數和描述
1

con(必填)

這是一個表示與 MySQL 伺服器連線的物件,您需要關閉它。

返回值

PHP mysqli_close() 函式返回一個布林值,成功時為true,失敗時為false

PHP 版本

此函式首次引入於 PHP 5 版本,並在所有後續版本中均有效。

示例

以下示例演示了mysqli_close() 函式的使用(以過程式風格)−

<?php
   $host = "localhost";
   $username  = "root";
   $passwd = "password";
   $dbname = "mydb";

   //Creating a connection
   $con = mysqli_connect($host, $username, $passwd, $dbname);

   //Closing the connection
   $res = mysqli_close($con);

   if($res){
      print("Connection Closed");
   }else{
      print("There is an issue while closing the connection");
   }
?>

這將產生以下結果−

Connection Closed

示例

在面向物件風格中,此函式的語法為$con->close(); 以下是此函式在面向物件風格中的示例 $minus;

<?php
   $host = "localhost";
   $username  = "root";
   $passwd = "password";
   $dbname = "mydb";

   //Creating a connection
   $con = new mysqli($host, $username, $passwd, $dbname);

   //Closing the connection
   $res = $con -> close();

   if($res){
      print("Connection Closed");
   }else{
      print("There is an issue while closing the connection");
   }
?>

這將產生以下結果−

Connection Closed

示例

這是另一個mysqli_close() 函式的示例−

<?php
   //Creating a connection
   $con = @mysqli_connect("localhost");
   $res = @mysqli_close($con);

   if($res){
      print("Connection closed Successfully");
   }else{
      print("Sorry there is an issue could close the connection ");
   }
?>

這將產生以下結果−

Sorry there is an issue could close the connection

示例

<?php
   $connection = @mysqli_connect("tutorailspoint.com", "use", "pass", "my_db");
   
   if (mysqli_connect_errno($connection)){
      echo "Failed to connect to MySQL: ".mysqli_connect_error();
   }else{
	   mysqli_close($connection);
   }   
?>

這將產生以下結果−

Failed to connect to MySQL: No connection could be made because the target machine actively refused it.
php_function_reference.htm
廣告