English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP 基础教程

PHP 高级教程

PHP & MySQL

PHP 参考手册

PHP mysqli_stmt_execute() 函数用法及示例

PHP MySQLi Référence Manuel

mysqli_stmt_execute()函数执行准备好的查询。

定义和用法

mysqli_stmt_execute()函数接受一个准备好的语句对象(使用prepare()函数创建)作为参数,并执行它,在执行时,任何存在的参数标记将自动替换为适当的数据。

在此函数之后,如果调用mysqli_stmt_affected_rows()函数(在UPDATE,DELETE,INSERT查询的情况下),则将获得受影响的行数。以同样的方式,如果调用mysqli_stmt_fetch()函数(在SELECT的情况下),将返回结果集。

语法

mysqli_stmt_execute($stmt);

参数

序号参数及说明
1

con(必需)

This is the object representing the prepared statement.

Return value

The PHP mysqli_stmt_execute() function returns a boolean value, true when successfultrue, fails tofalse.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Exemples en ligne

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;
   //Exécuter l'instruction
   mysqli_stmt_execute($stmt);
   print("The record has been updated......\n");
   //Fin de l'instruction
   mysqli_stmt_execute($stmt);
   //Fermer la connexion
   mysqli_close($con);
?>

Affichage des résultats

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)

Exemples en ligne

In object-oriented style, the syntax of this function is$stmt-> execute();。Here is an example of this function in 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("Création de la table.....\n");
   //Insérer des valeurs dans la table en utilisant les instructions préparées
   $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';
   //Exécuter l'instruction
   $stmt->execute();
   //Fin de l'instruction
   $stmt->close();
   //Fermer la connexion
   $con->close();
?>

Affichage des résultats

Création de la table.....

Exemples en ligne

Vous pouvez également exécuter des instructions créées parmysqli_stmt_prepare()Instructions créées par la fonction   -

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   mysqli_query($con, $query);
   print("Création de la table.....\n");
   //Initialisation de l'instruction
   $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("Insertion des enregistrements.....");
   //Exécuter l'instruction
   mysqli_stmt_execute($stmt);
   //Fin de l'instruction
   mysqli_stmt_close($stmt);
   //Fermer la connexion
   mysqli_close($con);
?>

Affichage des résultats

Création de la table.....
Insertion des enregistrements.....

PHP MySQLi Référence Manuel