mysqli_stmt_reset()函數(shù)重置預處理語句
mysqli_stmt_reset()函數(shù)接受一個準備好的語句對象(先前已打開)作為參數(shù),并對其進行重置,即它更改會重置錯誤,未緩沖的結(jié)果集和發(fā)送的數(shù)據(jù)。 查詢,綁定和存儲的結(jié)果集將不會更改。
mysqli_stmt_reset($stmt);
| 序號 | 參數(shù)及說明 |
|---|---|
| 1 | con(必需) 這是表示準備好的語句的對象。 |
PHP mysqli_stmt_reset()函數(shù)返回一個布爾值,成功時為true,失敗時為false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_reset()函數(shù)的用法(面向過程風格)-
<?php
$con = mysqli_connect("localhost", "root", "password", "mydb");
mysqli_query($con, "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");
mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
print("插入記錄.....\n");
//檢索表的內(nèi)容
$stmt = mysqli_prepare($con, "SELECT * FROM myplayers");
//執(zhí)行語句
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_reset($stmt);
if($res){
print("重置成功");
}
//將結(jié)果中的值綁定到變量
$res = mysqli_stmt_bind_result($stmt, $id, $fname, $lname, $pob, $country);
while (mysqli_stmt_fetch($stmt)) {
print("Id: ".$id."\n");
print("fname: ".$fname."\n");
print("lname: ".$lname."\n");
print("pob: ".$pob."\n");
print("country: ".$country."\n");
print("\n");
}
//結(jié)束語句
mysqli_stmt_close($stmt);
//關閉連接
mysqli_close($con);
?>輸出結(jié)果
插入記錄..... 重置成功
由于我們在中間重置了該語句,因此在沒有reset函數(shù)的情況下不會打印結(jié)果的內(nèi)容,該程序?qū)⑸梢韵螺敵?
插入記錄..... Reset Successful E:\PHPExamples>php procedure_oriented.php 創(chuàng)建表..... 插入記錄..... Id: 1 fname: Sikhar lname: Dhawan pob: Delhi country: India Id: 2 fname: Jonathan lname: Trott pob: CapeTown country: SouthAfrica
在面向?qū)ο箫L格中,此函數(shù)的語法為$stmt-> close();。以下是面向?qū)ο箫L格中此函數(shù)的示例;
<?php
//建立連接
$con = new mysqli("localhost", "root", "password", "mydb");
//Creating a table
$con -> query("CREATE TABLE players(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
print("創(chuàng)建表.....\n");
//使用預準備語句將值插入到表中
$stmt = $con -> prepare( "INSERT INTO players values(?, ?, ?, ?, ?)");
$res = $stmt->reset();
if($res){
print("Reset Successful");
}
//Binding values to the parameter markers
$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();
//關閉連接
$con->close();
?>輸出結(jié)果
創(chuàng)建表..... Reset Successful
并且玩家表的內(nèi)容將為空-
mysql> drop table players; Query OK, 0 rows affected (0.26 sec)
如果刪除reset()函數(shù)并執(zhí)行上述程序,那么players表的內(nèi)容將如下所示:
mysql> select * from players; +------+------------+-----------+----------------+---------+ | ID | First_Name | Last_Name | Place_Of_Birth | Country | +------+------------+-----------+----------------+---------+ | 1 | Shikhar | Dhawan | Delhi | India | +------+------------+-----------+----------------+---------+ 1 row in set (0.00 sec)