目录

PHP mysqli affected_rows() 函数

❮ PHP MySQLi 参考

示例 - 面向对象风格

返回不同查询中受影响的行数:

<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// Perform queries and print out affected rows
$mysqli -> query("SELECT * FROM Persons");
echo "Affected rows: " . $mysqli -> affected_rows;

$mysqli -> query("DELETE FROM Persons WHERE Age>32");
echo "Affected rows: " . $mysqli -> affected_rows;

$mysqli -> close();
?>

查看底部的程序样式示例。


定义和用法

affected_rows / mysqli_affected_rows() 函数返回先前 SELECT、INSERT、UPDATE、REPLACE 或 DELETE 查询中受影响的行数。


语法

面向对象风格:

$mysqli -> affected_rows

程序风格:

mysqli_affected_rows( connection)

参数值

Parameter Description
connection Required. Specifies the MySQL connection to use

技术细节

返回值: 受影响的行数。 -1表示查询返回错误
PHP 版本: 5+

示例 - 程序风格

返回不同查询中受影响的行数:

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}

// Perform queries and print out affected rows
mysqli_query($con, "SELECT * FROM Persons");
echo "Affected rows: " . mysqli_affected_rows($con);

mysqli_query($con, "DELETE FROM Persons WHERE Age>32");
echo "Affected rows: " . mysqli_affected_rows($con);

mysqli_close($con);
?>


❮ PHP MySQLi 参考