English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
mysqli_stmt_reset()函数重置预处理语句
mysqli_stmt_reset()函数接受一个准备好的语句对象(先前已打开)作为参数,并对其进行重置,即它更改会重置错误,未缓冲的结果集和发送的数据。查询,绑定和存储的结果集将不会更改。
mysqli_stmt_reset($stmt);
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是表示准备好的语句的对象。 |
PHP mysqli_stmt_reset()函数返回一个布尔值,成功时为true,失败时为false。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_reset()函数的用法(面向过程风格)-
<?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("创建表.....\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"); //检索表的内容 $stmt = mysqli_prepare($con, "SELECT * FROM myplayers"); //执行语句 mysqli_stmt_execute($stmt); $res = mysqli_stmt_reset($stmt); if($res){ print("Reset successful"); } //Binding the values in the result to variables $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"); } //结束语句 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
Inserting records..... Reset successful
Since we reset the statement in the middle, the content of the result will not be printed without the reset function, and the program will generate the following output-
Inserting records..... Reset Successful E:\PHPExamples>php procedure_oriented.php 创建表..... Inserting records..... Id: 1 fname: Sikhar lname: Dhawan pob: Delhi country: India Id: 2 fname: Jonathan lname: Trott pob: CapeTown country: SouthAfrica
In the object-oriented style, the syntax of this function is$stmt->close();.The following is an example of this function in an object-oriented style;
<?php //Establishing connection $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("创建表.....\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'; //执行语句 $stmt->execute(); //结束语句 $stmt->close(); //关闭连接 $con->close(); ?>
输出结果
创建表..... Reset Successful
并且玩家表的内容将为空-
mysql> drop table players; Query OK, 0 rows affected (0.26 sec)
如果删除reset()函数并执行上述程序,那么players表的内容将如下所示:
mysql> select * from players; +------+------------+-----------+----------------+---------+ | ID | First_Name | Last_Name | Place_Of_Birth | Country | +------+------------+-----------+----------------+---------+ | 1 | Shikhar | Dhawan | Delhi | India | +------+------------+-----------+----------------+---------+ 1 سطر في المجموعة (0.00 ثانية)