mysqli_stmt_close()函數(shù)結(jié)束預(yù)處理語句。
mysqli_stmt_close()函數(shù)接受一個(gè)預(yù)處理的語句對象(先前已打開)作為參數(shù),然后將其關(guān)閉。
您不能使用此函數(shù)關(guān)閉持久連接。
mysqli_stmt_close($stmt);
| 序號 | 參數(shù)及說明 |
|---|---|
| 1 | stmt(必需) 這是表示準(zhǔn)備好的語句的對象。 |
PHP mysqli_stmt_close()函數(shù)返回一個(gè)布爾值,成功時(shí)為true,失敗時(shí)為false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
假設(shè)我們已經(jīng)在MySQL數(shù)據(jù)庫中創(chuàng)建了一個(gè)名為employee的表,其內(nèi)容如下:
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()函數(shù)的用法(面向過程風(fēng)格)-
<?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;
//執(zhí)行語句
mysqli_stmt_execute($stmt);
print("Records Updated......\n");
//結(jié)束語句
mysqli_stmt_close($stmt);
//關(guān)閉連接
mysqli_close($con);
?>輸出結(jié)果
Records Updated......
執(zhí)行完上述程序后,employee表的內(nèi)容如下:
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)
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$stmt-> close();。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
<?php
//建立連接
$con = new mysqli("localhost", "root", "password", "mydb");
//創(chuàng)建一個(gè)表
$con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
print("創(chuàng)建表.....\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';
//執(zhí)行語句
$stmt->execute();
//結(jié)束語句
$stmt->close();
//關(guān)閉連接
$con->close();
?>輸出結(jié)果
創(chuàng)建表.....
您也可以關(guān)閉由mysqli_stmt_prepare()函數(shù)創(chuàng)建的語句 -
<?php
$con = mysqli_connect("localhost", "root", "password", "mydb");
$query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)";
mysqli_query($con, $query);
print("創(chuàng)建表.....\n");
//初始化語句
$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("插入記錄.....");
//執(zhí)行語句
mysqli_stmt_execute($stmt);
//結(jié)束語句
mysqli_stmt_close($stmt);
//關(guān)閉連接
mysqli_close($con);
?>輸出結(jié)果
創(chuàng)建表..... 插入記錄.....