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
广告