您的位置:首页 > Web前端

mysqli_affected_rows 与 mysqli_num_rows的不同之处

2017-05-01 15:56 344 查看
来源于http://stackoverflow.com/questions/25555758/what-is-the-difference-between-mysqli-affected-rows-and-mysqli-num-rows

问题如下:

down
votefavorite

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?

解答如下:

0down
votefavorite

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
INSERT
,
UPDATE
,
REPLACE
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
.

0down
votefavorite

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?

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: