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
广告