當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP mysqli_stmt_close()用法及代碼示例



定義和用法

這個mysqli_stmt_close()函數接受一個準備好的語句對象(之前打開的)作為參數,並關閉它。

您無法使用此函數關閉持久連接。

用法

mysqli_stmt_close($stmt);

參數

Sr.No 參數及說明
1

stmt(Mandatory)

這是一個表示準備好的語句的對象。

返回值

PHP mysqli_stmt_close() 函數返回一個布爾值,成功時為真,失敗時為假。

PHP版本

這個函數最初是在 PHP 版本 5 中引入的,適用於所有後續版本。

示例

假設我們在 MySQL 數據庫中創建了一個名為employee 的表,內容如下:

mysql> select * from employee;
+------------+--------------+------+------+--------+
| FIRST_NAME | LAST_NAME    | AGE  | SEX  | INCOME |
+------------+--------------+------+------+--------+
| Vinay      | Bhattacharya |   20 | M    |  16000 |
| Sharukh    | Sheik        |   25 | M    |  18300 |
| Trupthi    | Mishra       |   24 | F    |  36000 |
| Sheldon    | Cooper       |   25 | M    |  12256 |
| Sarmista   | Sharma       |   28 | F    |  15000 |
+------------+--------------+------+------+--------+
5 rows in set (0.00 sec)

以下示例演示了 mysqli_stmt_close() 函數的用法(程序風格) -

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   $stmt = mysqli_prepare($con, "UPDATE employee set INCOME=INCOME-? where INCOME>?");
   mysqli_stmt_bind_param($stmt, "si", $reduct, $limit);
   $limit = 16000;
   $reduct = 5000;

   //Executing the statement
   mysqli_stmt_execute($stmt);
   print("Records Updated......\n");

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果 -

Records Updated......

執行上述程序後,employee 表的內容如下 -

mysql> select * from employee;
+------------+--------------+------+------+--------+
| FIRST_NAME | LAST_NAME    | AGE  | SEX  | INCOME |
+------------+--------------+------+------+--------+
| Vinay      | Bhattacharya |   20 | M    |  16000 |
| Sharukh    | Sheik        |   25 | M    |  13300 |
| Trupthi    | Mishra       |   24 | F    |  31000 |
| Sheldon    | Cooper       |   25 | M    |  12256 |
| Sarmista   | Sharma       |   28 | F    |  15000 |
+------------+--------------+------+------+--------+
5 rows in set (0.00 sec)

示例

在麵向對象風格中,這個函數的語法是 $stmt->close();以下是麵向對象樣式 $minus 中此函數的示例;

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Creating a table
   $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created.....\n");

   //Inserting values into the table using prepared statement
   $stmt = $con -> prepare( "INSERT INTO myplayers values(?, ?, ?, ?, ?)");
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';

   //Executing the statement
   $stmt->execute();

   //Closing the statement
   $stmt->close();

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

這將產生以下結果 -

Table Created.....

示例

您還可以關閉由 mysqli_stmt_prepare() 函數創建的語句 -

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   mysqli_query($con, $query);
   print("Table Created.....\n");
 
   //Initializing the statement
   $stmt = mysqli_stmt_init($con);

   mysqli_stmt_prepare($stmt, "INSERT INTO Test values(?, ?)");
   mysqli_stmt_bind_param($stmt, "si", $Name, $Age);
   $Name = 'Raju';
   $Age = 25;
   print("Record Inserted.....");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果 -

Table Created.....
Record Inserted.....

相關用法


注:本文由純淨天空篩選整理自 PHP mysqli_stmt_close() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。