当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。