Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_stmt_close() Function.
The built-in mysqli_stmt_close() function in PHP accepts a prepared statement object (previously opened) as a parameter, and closes it.
You cannot close persistent connections using this PHP function.
The built-in mysqli_stmt_close() function in PHP accepts a prepared statement object (previously opened) as a parameter, and closes it.
You cannot close persistent connections using this PHP function.
Syntax
Following below is the syntax to use this function -
mysqli_stmt_close($stmt);
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | stmt(Mandatory) This is an object representing a prepared statement. |
Return Value
This built-in PHP function returns TRUE on success and FALSE on failure.
PHP Version
This PHP function was first introduced in PHP version 5 and works in all the later versions.
Example1
Assuming we have created a table named employee in the MYSQL database with the following contents $minus;
mysql> select * from employee; +------------+--------------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+--------------+------+------+--------+ | Kennedy | Nkpara | 27 | M | 16000 | | Queen | Douglas | 25 | F | 18300 | | Stephanie | Francis | 24 | F | 36000 | | Paul | Francis | 30 | M | 12256 | | Justice | Donald | 28 | M | 15000 | +------------+--------------+------+------+--------+ 5 rows in set (0.00 sec)
The following below is an example which demonstrates the usage of the built-in PHP mysqli_stmt_close() function (in a 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; //Executing the statement mysqli_stmt_execute($stmt); print("Records Updated......\n"); //Closing the statement mysqli_stmt_close($stmt); //Closing the connection mysqli_close($con); ?>
Output
When the above code is executed, it will produce the following result -
Records Updated.....
After executing the above program, the contents of the employee table will be as shown below -
mysql> select * from employee; +------------+--------------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+--------------+------+------+--------+ | Kennedy | Nkpara | 27 | M | 16000 | | Queen | Douglas | 25 | F | 13300 | | Stephanie | Francis | 24 | F | 31000 | | Paul | Francis | 30 | M | 12256 | | Justice | Donald | 28 | M | 15000 | +------------+--------------+------+------+--------+ 5 rows in set (0.00 sec)
Example2
In an object oriented style the syntax of this function is $stmt->close(); Following is the example of this function in an object oriented style $minus;
<?php //Creating a connection $con = new mysqli("localhost", "root", "password", "mydb"); //Creating a table $con -> query("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"); //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 = 'Kennedy'; $lname = 'Nkpara'; $pob = 'PortHarcourt'; $country = 'Nigeria'; //Executing the statement $stmt->execute(); //Closing the statement $stmt->close(); //Closing the connection $con->close(); ?>
Output
When the above code is executed, it will produce the following result -
Table Created.....
Example3
You can also close a statement created by the PHP mysqli_stmt_prepare() function -
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; mysqli_query($con, $query); print("Table Created.....\n"); //Initializing the statement $stmt = mysqli_stmt_init($con); mysqli_stmt_prepare($stmt, "INSERT INTO Test values(?, ?)"); mysqli_stmt_bind_param($stmt, "si", $Name, $Age); $Name = 'Paul'; $Age = 30; print("Record Inserted....."); //Executing the statement mysqli_stmt_execute($stmt); //Closing the statement mysqli_stmt_close($stmt); //Closing the connection mysqli_close($con); ?>
Output
When the above code is executed, it will produce the following result below -
Table Created..... Records Inserted.....
Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP mysqli_stmt_data_seek() Function.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.