PHP mysqli affected_rows 函数
示例 - 面向对象的样式
从不同的查询中输出所影响记录行数:
<?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();
?>
$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() 函数返回前一次 MySQL 操作(SELECT、INSERT、UPDATE、REPLACE、DELETE)所影响的记录行数。
语法
面向对象的风格:
$mysqli -> affected_rows
程序风格:
mysqli_affected_rows(connection)
参数值
参数 | 描述 |
---|---|
connection | 必需。规定要使用的 MySQL 连接。 |
技术细节
返回值: | 一个 > 0 的整数表示所影响的记录行数。0 表示没有受影响的记录。-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);
?>
$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);
?>