English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
mysqli_stmt_reset() function resets the prepared statement
mysqli_stmt_reset()The function accepts a prepared statement object (previously opened) as a parameter and resets it, i.e., it resets errors, unbuffered result sets, and sent data. The query, bound, and stored result sets will not change.
mysqli_stmt_reset($stmt);
Serial Number | Parameters and Description |
---|---|
1 | con (required) This is the object representing the prepared statement. |
The PHP mysqli_stmt_reset() function returns a boolean value, true when it succeeds.true, and false when it fails.false.
This function was initially introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_stmt_reset()Function Usage (Procedural Style)-
<?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(255255)) print("Creating table.....\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("Inserting record.....\n"); //Retrieve the content of the table $stmt = mysqli_prepare($con, "SELECT * FROM myplayers); //Execute statement 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"); } //End statement mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output Result
Inserting record..... 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 record..... Reset Successful E:\PHPExamples>php procedure_oriented.php Creating table..... Inserting record..... Id: 1 fname: Sikhar lname: Dhawan pob: Delhi country: India Id: 2 fname: Jonathan lname: Trott pob: Cape Town country: South Africa
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 //Establish 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(255255)) print("Creating table.....\n"); //Inserting values into the table using prepared statements $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'; //Execute statement $stmt->execute(); //End statement $stmt->close(); //Close connection $con->close(); ?>
Output Result
Creating table..... Reset Successful
and the content of the player table will be empty-
mysql> drop table players; Query OK, 0 rows affected (0.26 sec)
If the reset() function is deleted and the above program is executed, the content of the players table will be as follows:
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)