PHP mysqli_stmt_execute() 函式



定義和用法

mysqli_stmt_execute() 函式接受一個預處理語句物件(使用 prepare() 函式建立)作為引數,並執行它。

呼叫時,所有引數標記都將被繫結資料替換。此函式呼叫後,如果呼叫 mysqli_stmt_affected_rows() 函式(對於 UPDATE、DELETE、INSERT 查詢),將獲得受影響的行數。同樣,如果呼叫 mysqli_stmt_fetch() 函式(對於 SELECT),則返回結果集。

語法

mysqli_stmt_execute($stmt);

引數

序號 引數 & 描述
1

con(必填)

這是一個代表預處理語句的物件。

返回值

PHP mysqli_stmt_execute() 函式返回一個布林值,成功時為 true,失敗時為 false

PHP 版本

此函式首次引入於 PHP 5 版本,並在所有後續版本中均有效。

示例

假設我們在 MySQL 資料庫中建立了一個名為 employee 的表,其內容如下:

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)

以下示例演示了 mysqli_stmt_execute() 函式的用法(過程式風格):

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

這將產生以下結果:

Records Updated......

程式執行後,employee 表的內容將如下所示:

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)

示例

在面向物件風格中,此函式的語法為 $stmt->execute(); 以下是此函式在面向物件風格中的示例:

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

這將產生以下結果:

Table Created.....

示例

您還可以執行由 mysqli_stmt_prepare() 函式建立的語句:

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

這將產生以下結果:

Table Created.....
Record Inserted.....
php_function_reference.htm
廣告