• PHP Video Tutorials

PHP mysqli_stmt_execute() Function


Advertisements

Definition and Usage

The mysqli_stmt_execute() function accepts a prepared statement object (created using the prepare() function) as a parameter, and executes it.

When invoked all the parameter markers will be replaced with the bounded data. After this function if you invoke mysqli_stmt_affected_rows() function (in case of UPDATE, DELETE, INSERT queries) you will get the number of rows affected. In the same way if you invoke the mysqli_stmt_fetch() function (in-case of SELECT) a result set is returned.

Syntax

mysqli_stmt_execute($stmt);

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a prepared statement.

Return Values

The PHP mysqli_stmt_execute() function returns a boolean value which is true on success and false on failure.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Assume 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 |
+------------+--------------+------+------+--------+
| 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)

Following example demonstrates the usage of the mysqli_stmt_execute() function (in procedural style) −

<?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;

   //Executing the statement
   mysqli_stmt_execute($stmt);
   print("Records Updated......\n");

   //Closing the statement
   mysqli_stmt_execute($stmt);

   //Closing the connection
   mysqli_close($con);
?>

This will produce following result −

Records Updated......

After the execution of the above program the contents of the employee table will be 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)

Example

In object oriented style the syntax of this function is $stmt->execute(); Following is the example of this function in 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 = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';

   //Executing the statement
   $stmt->execute();

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

This will produce following result −

Table Created.....

Example

You can also execute a statement created by the 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 = 'Raju';
   $Age = 25;
   print("Record Inserted.....");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

This will produce following result −

Table Created.....
Record Inserted.....
php_function_reference.htm

Useful Video Courses


Video

PHP Online Training

45 Lectures 9 hours

Malhar Lathkar

Video

Become PHP Facebook Developer: Password-Less Authentication

34 Lectures 4 hours

Syed Raza

Video

Ultimate PHP & MySQL Web Development Course & OOP Coding

84 Lectures 5.5 hours

Frahaan Hussain

Video

Learn PHP from Scratch

17 Lectures 1 hours

Nivedita Jain

Video

E Commerce Website Development In PHP With PDO

100 Lectures 34 hours

Azaz Patel

Video

PHP in Telugu

43 Lectures 5.5 hours

Vijay Kumar Parvatha Reddy

Advertisements