PHP mysqli_affected_rows() 函式



定義和用法

mysqli_affected_rows() 函式返回先前操作受影響的行數,如果在 INSERT、UPDATE、REPLACE 或 DELETE 查詢後呼叫。

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

語法

mysqli_affected_rows($con)

引數

序號 引數及描述
1

con(必填)

這是一個表示與 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_function_reference.htm
廣告