English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_close() function ends the preprocessed statement.
mysqli_stmt_close()The function accepts a preprocessed statement object (previously opened) as a parameter and then closes it.
You cannot use this function to closePersistent Connection.
mysqli_stmt_close($stmt);
Serial Number | Parameter and Description |
---|---|
1 | stmt(mandatory) This is the object representing the prepared statement. |
The PHP mysqli_stmt_close() function returns a boolean value, which is true when successfultrue, on failure it isfalse.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
Assuming that we have already created a table named employee in the MySQL database, whose content is as follows:
mysql> select * from employee; +------------+--------------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+--------------+------+------+--------+ | Vinay | Bhattacharya | 20 | M | 16000 | | Sharukh | Sheik | 25 | M | 18300 | | Trupthi | Mishra | 24 | F | 36000 | | Sheldon | Cooper | 25 | M | 12256 | | Sarmista | Sharma | 28 | F | 15000 | +------------+--------------+------+------+--------+ 5 rows in set (0.00 sec)
The following example demonstratesmysqli_stmt_close()Usage of the function (procedural style)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $stmt = mysqli_prepare($con, "UPDATE employee set INCOME=INCOME-? where INCOME>?"); mysqli_stmt_bind_param($stmt, "si", $reduct, $limit); $limit = 16000; $reduct = 5000; //Execute statement mysqli_stmt_execute($stmt); print("Records Updated......\n"); //End statement mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output result
Records Updated......
After executing the above program:employeeThe content of the table is as follows:
mysql> select * from employee; +------------+--------------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+--------------+------+------+--------+ | Vinay | Bhattacharya | 20 | M | 16000 | | Sharukh | Sheik | 25 | M | 13300 | | Trupthi | Mishra | 24 | F | 31000 | | Sheldon | Cooper | 25 | M | 12256 | | Sarmista | Sharma | 28 | F | 15000 | +------------+--------------+------+------+--------+ 5 rows in set (0.00 sec)
In an object-oriented style, the syntax of this function is$stmt-> close();.Here is an example of this function in an object-oriented style;
<?php //Establish a connection $con = new mysqli("localhost", "root", "password", "mydb"); //Create a table $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255)) print("Create table.....\n"); //Inserting values into the table using prepared statement $stmt = $con -> prepare("INSERT INTO myplayers values(?, ?, ?, ?, ?"); $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
Create table.....
You can also close the connection made bymysqli_stmt_prepare()Statement created by the function -
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT); mysqli_query($con, $query); print("Create table.....\n"); //Initialize statement $stmt = mysqli_stmt_init($con); mysqli_stmt_prepare($stmt, 'INSERT INTO Test values(?, ?)'); mysqli_stmt_bind_param($stmt, 'si', $Name, $Age); $Name = 'Raju'; $Age = 25; print("Insert record....."); //Execute statement mysqli_stmt_execute($stmt); //End statement mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output result
Create table..... Insert record.....