PHP mysqli_stmt_affected_rows() 函数



定义和用法

mysqli_stmt_affected_rows() 函数返回最近执行的语句影响的行数(更改、删除、插入)。

此函数仅在 INSERT、UPDATE 或 DELETE 语句之后调用时才能正常工作。如果您需要知道 SELECT 查询影响的行数,则需要使用 mysqli_stmt_num_rows() 函数。

语法

mysqli_stmt_affected_rows($stmt)

参数

序号 参数及描述
1

stmt(必填)

这是一个表示执行 SQL 查询的语句的对象。

返回值

PHP mysqli_stmt_affected_rows() 函数返回一个整数值,指示先前(INSERT、UPDATE、REPLACE 或 DELETE)操作影响的行数。

如果语句有错误,此函数返回 -1。如果没有受影响的行,此函数返回 0

PHP 版本

此函数首次在 PHP 5 版中引入,并在所有后续版本中都能正常工作。

示例

假设我们在 MySQL 数据库中创建了一个名为 employee 的表,其内容如下所示 $minus;

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_function_reference.htm
广告