當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。