English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_execute() function executes the prepared query.
mysqli_stmt_execute()The function accepts a prepared statement object (created using the prepare() function) as a parameter and executes it, replacing any existing parameter markers with appropriate data automatically during execution.
After this function, if the mysqli_stmt_affected_rows() function (in the case of UPDATE, DELETE, INSERT queries) is called, the number of affected rows will be obtained. In the same way, if the mysqli_stmt_fetch() function (in the case of SELECT) is called, the result set will be returned.
mysqli_stmt_execute($stmt);
Serial Number | Parameters and Description |
---|---|
1 | con(necessary) This is the object representing the prepared statement. |
PHP mysqli_stmt_execute() function returns a boolean value, true when it succeedstrue, when it fails 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, which contains the following content:
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_execute()Usage of the function (procedural style), executing and preparing an update statement:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $stmt = mysqli_prepare($con, "UPDATE employee set INCOME=INCOME-? where INCOME >?"); mysqli_stmt_execute($stmt, "si", $reduct, $limit); $limit = 16000; $reduct = 5000; //Execute statement mysqli_stmt_execute($stmt); print("The record has been updated......\n"); //End statement mysqli_stmt_execute($stmt); //Close connection mysqli_close($con); ?>
Output result
The record has been 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 the object-oriented style, the syntax of this function is$stmt-> execute();.Here is an example of this function in an object-oriented style, executing and preparing an insert statement
<?php //Establishing 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("Create table.....\n"); //Use prepared statements to insert values into the table $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 execute statements created 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.....