当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP mysqli_stmt_reset()用法及代码示例



定义和用法

这个mysqli_stmt_reset()函数接受一个准备好的语句对象(之前打开的)作为参数,并重置它,即它更改重置错误、未缓冲的结果集和发送的数据。查询、绑定和存储的结果集不会改变。

用法

mysqli_stmt_reset($stmt);

参数

Sr.No 参数及说明
1

con(Mandatory)

这是一个表示准备好的语句的对象。

返回值

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

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();以下是面向对象样式 $minus 中此函数的示例;

<?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

玩家表的内容将为空 -

mysql> drop table players;
Query OK, 0 rows affected (0.26 sec)

如果删除 reset() 函数并执行上述程序,则玩家表的内容如下 -

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 mysqli_stmt_reset() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。