PHP mysqli_stmt_reset() 函数



定义和用法

mysqli_stmt_reset() 函数接受一个预处理语句对象(先前已打开)作为参数,并将其重置,即重置错误、无缓冲结果集和已发送的数据。查询、绑定和存储的结果集将不会更改。

语法

mysqli_stmt_reset($stmt);

参数

序号 参数及描述
1

con(必填)

这是一个表示预处理语句的对象。

返回值

PHP mysqli_stmt_reset() 函数返回一个布尔值,成功时为 true,失败时为 false

PHP 版本

此函数首次在 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("Table Created.....\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("Record Inserted.....\n");

   //Retrieving the contents of the table
   $stmt = mysqli_prepare($con, "SELECT * FROM myplayers");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   $res = mysqli_stmt_reset($stmt);
   if($res){
      print("Reset Successful");	
   }

   //Binding values in 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");

   }
   //Closing the statement
   mysqli_stmt_close($stmt);

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

这将产生以下结果:

Record Inserted.....
Reset Successful

由于我们在中间重置了语句,结果的内容不会打印出来。如果没有重置函数,此程序将生成以下输出:

Record Inserted.....
Reset Successful
E:\PHPExamples>php procedure_oriented.php
Table Created.....
Record Inserted.....
Id: 1
fname: Sikhar
lname: Dhawan
pob: Delhi
country: India

Id: 2
fname: Jonathan
lname: Trott
pob: CapeTown
country: SouthAfrica

示例

在面向对象风格中,此函数的语法为 $stmt->close(); 以下是此函数在面向对象风格中的示例:

<?php
   //Creating a 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("Table Created.....\n");

   //Inserting values into the table using prepared statement
   $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';

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

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

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

这将产生以下结果:

Table Created.....
Reset Successful

并且 players 表的内容将为空:

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 row in set (0.00 sec)
php_function_reference.htm
广告