mysqli_affected_rows 与 mysqli_num_rows的不同之处

本文探讨了PHP中mysqli_num_rows与mysqli_affected_rows两个函数的区别。前者返回SELECT查询结果集中的行数,而后者返回最近一次INSERT、UPDATE、REPLACE或DELETE操作所影响的行数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源于http://stackoverflow.com/questions/25555758/what-is-the-difference-between-mysqli-affected-rows-and-mysqli-num-rows


问题如下:



The PHP docs for mysqli_num_rows says

Returns the number of rows in the result set.

The PHP docs for mysqli_affected_rows says

Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query.

_num_rows is called on a result, and _affected_rows is called on a connection. Since I think they do the same thing(correct this assumption if I'm wrong), I'm wondering whether one works better than the other, and which situations would call for which function.

Aren't number of rows affected and number of rows in the result set synonymous?


解答如下:

The PHP docs for mysqli_num_rows says

Returns the number of rows in the result set.

The PHP docs for mysqli_affected_rows says

Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query.

_num_rows is called on a result, and _affected_rows is called on a connection. Since I think they do the same thing(correct this assumption if I'm wrong), I'm wondering whether one works better than the other, and which situations would call for which function.

Aren't number of rows affected and number of rows in the result set synonymous?


num_rows tells you how many rows there are in the result set you just selected with a SELECT query. affected_rows tells you how many rows where affected by an INSERTUPDATEREPLACE or DELETE query. The difference is obvious:

$resultSet = mysqli_query($c, 'SELECT ...');
echo mysqli_num_rows($resultSet);

SELECT result set goes into num_rows.

mysqli_query($c, 'UPDATE ...');
echo mysqli_affected_rows($c);

No result set, no num_rows.



The PHP docs for mysqli_num_rows says

Returns the number of rows in the result set.

The PHP docs for mysqli_affected_rows says

Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query.

_num_rows is called on a result, and _affected_rows is called on a connection. Since I think they do the same thing(correct this assumption if I'm wrong), I'm wondering whether one works better than the other, and which situations would call for which function.

Aren't number of rows affected and number of rows in the result set synonymous?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值