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
廣告