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


PHP mysqli_affected_rows()用法及代碼示例


定義和用法

這個mysqli_affected_rows()如果在 INSERT、UPDATE、REPLACE 或 DELETE 查詢之後調用,則函數返回受前一操作影響的行數。

在 select 語句之後使用時,此函數返回行數。

用法

mysqli_affected_rows($con)

參數

Sr.No 參數及說明
1

con(Mandatory)

這是一個表示與 MySQL 服務器的連接的對象。

返回值

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

如果前一個查詢有錯誤,則此函數返回-1.如果沒有受影響的行或前一個查詢/操作不是上述之一,則此函數返回0

PHP版本

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

示例

以下示例演示了 mysqli_affected_rows() 函數的用法(程序風格) -

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

   //Query to retrieve all the rows of employee table
   mysqli_query($con, "SELECT * FROM employee");

   //Effected rows
   $rows = mysqli_affected_rows($con);
   print("Number of affected rows:".$rows);

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

這將產生以下結果 -

Number of affected rows:5

示例

在麵向對象的風格中,這個函數的語法是 $con -> affected_rows,其中,$con 是連接對象 -

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

   //Query to retrieve all the rows of employee table
   $con -> query("SELECT * FROM employee");

   //Number of affected rows
   $rows = $con -> affected_rows;
   print("Number of affected rows:".$rows);

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

這將產生以下結果 -

Number of affected rows:5

示例

讓我們檢查這個函數的返回值,當它之前沒有(指定的)查詢時,當查詢有錯誤或它不影響任何行時 -

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

   $rows1 = mysqli_affected_rows($con);
   print("Rows Affected (no specified previous operations):".$rows1."\n");

   //Query to retrieve all the rows of employee table
   mysqli_query($con, "SELECT * FORM employee");
   $rows2 = mysqli_affected_rows($con);
   print("Rows Affected (when query has error):".$rows2."\n");

   //Query to retrieve all the rows of employee table
   mysqli_query($con, "SELECT  FIRST_NAME FROM employee WHERE AGE <=19");
   $rows3 = mysqli_affected_rows($con);
   print("Rows Affected (when query does nothing):".$rows3."\n");

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

這將產生以下結果 -

Rows Affected (no specified previous operations):0
Rows Affected (when query has error):-1
Rows Affected (when query does nothing):0

示例

以下示例演示了 mysqli_affected_rows 函數與 SELECT、UPDATE、INSERT 和 DELETE 查詢的用法 -

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

   //Query to SELECT all the rows of the employee table
   mysqli_query($con, "SELECT * FROM employee where INCOME > 8000");
   print("Rows Affected by SELECT query:".mysqli_affected_rows($con)."\n");

   //Query to UPDATE the rows of the employee table
   mysqli_query($con, "UPDATE employee set INCOME=INCOME+5000 where FIRST_NAME in ('Ramya', 'Trupthi', 'Sarmista')");
   print("Rows Affected by UPDATE query:".mysqli_affected_rows($con)."\n");

   //Query to INSERT a row into the employee table
   mysqli_query($con, "INSERT INTO employee VALUES ('Archana', 'Mohonthy', 30, 'M', 13000, 106)");
   print("Rows Affected by INSERT query:".mysqli_affected_rows($con)."\n");

   //Query to DELETE rows of the employee table
   mysqli_query($con, "DELETE FROM employee where AGE > 25");
   print("Rows Affected by DELETE query:".mysqli_affected_rows($con)."\n");

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

這將產生以下結果 -

Rows Affected by SELECT query:4
Rows Affected by UPDATE query:3
Rows Affected by INSERT query:1
Rows Affected by DELETE query:3

相關用法


注:本文由純淨天空篩選整理自 PHP mysqli_affected_rows() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。