當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP mysqli_stmt_affected_rows()用法及代碼示例



定義和用法

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

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

用法

mysqli_stmt_affected_rows($stmt)

參數

Sr.No 參數及說明
1

stmt(Mandatory)

這是一個對象,表示執行 SQL 查詢的語句。

返回值

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

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

PHP版本

這個函數最初是在 PHP 版本 5 中引入的,適用於所有後續版本。

示例

假設我們在 MySQL 數據庫中創建了一個名為employee 的表,內容如下:

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 mysqli_stmt_affected_rows() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。