PHP mysqli_stmt_affected_rows() 函式



定義和用法

mysqli_stmt_affected_rows() 函式返回最近執行的語句影響的行數(更改、刪除、插入)。

此函式僅在 INSERT、UPDATE 或 DELETE 語句之後呼叫時才能正常工作。如果您需要知道 SELECT 查詢影響的行數,則需要使用 mysqli_stmt_num_rows() 函式。

語法

mysqli_stmt_affected_rows($stmt)

引數

序號 引數及描述
1

stmt(必填)

這是一個表示執行 SQL 查詢的語句的物件。

返回值

PHP mysqli_stmt_affected_rows() 函式返回一個整數值,指示先前(INSERT、UPDATE、REPLACE 或 DELETE)操作影響的行數。

如果語句有錯誤,此函式返回 -1。如果沒有受影響的行,此函式返回 0

PHP 版本

此函式首次在 PHP 5 版中引入,並在所有後續版本中都能正常工作。

示例

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

mysql> select * from employee;
+------------+--------------+------+------+--------+
| FIRST_NAME | LAST_NAME    | AGE  | SEX  | INCOME |
+------------+--------------+------+------+--------+
| Vinay      | Bhattacharya |   20 | M    |  21000 |
| Sharukh    | Sheik        |   25 | M    |  23300 |
| Trupthi    | Mishra       |   24 | F    |  51000 |
| Sheldon    | Cooper       |   25 | M    |   2256 |
| Sarmista   | Sharma       |   28 | F    |  15000 |
+------------+--------------+------+------+--------+
5 rows in set (0.00 sec)

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

<?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 = 20000;
   $reduct = 5000;

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

   //Affected rows
   $count = mysqli_stmt_affected_rows($stmt);

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);

   print("Rows affected ".$count);
?>

這將產生以下結果 -

Records Updated......
Rows affected 3

示例

在面向物件風格中,此函式的語法為 $con->affected_rows; 以下是此函式在面向物件風格中的示例 $minus;

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Table Created.....\n");
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Records Inserted.....\n");

   $stmt = $con -> prepare( "DELETE FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   print("Records Deleted.....\n");

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

   //Affected rows
   $count = $stmt ->affected_rows;
   print("Rows affected ".$count);

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

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

?>

這將產生以下結果 -

Table Created.....
Records Inserted.....
Records Deleted.....
Rows affected 2

示例

讓我們檢查一下如果查詢不影響任何行,此函式的返回值 -

<?php
   $con = @mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Table Created.....\n");
   mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Records Inserted.....\n");

   $stmt = mysqli_prepare($con, "DELETE FROM test where Age<?");
   mysqli_stmt_bind_param($stmt, "i", $num);
   $num = 8;
   //Executing the statement
   mysqli_stmt_execute($stmt);
   //Affected rows
   $count = mysqli_stmt_affected_rows($stmt);
   print("Rows affected (when query does nothing): ".$count);

   //Closing the statement
   mysqli_stmt_close($stmt);
   //Closing the connection
   mysqli_close($con);

?>

這將產生以下結果 -

Table Created.....
Records Inserted.....
Rows affected (when query does nothing): 0
php_function_reference.htm
廣告