25.2.3.1. mysql_affected_rows()
my_ulonglong mysql_affected_rows(MYSQL
*mysql)
Description
Returns the number of rows changed by the last
UPDATE
, deleted by the last
DELETE
or inserted by the last
INSERT
statement. May be called immediately
after mysql_query()
for
UPDATE
, DELETE
, or
INSERT
statements. For
SELECT
statements,
mysql_affected_rows()
works like
mysql_num_rows()
.
Return Values
An integer greater than zero indicates the number of rows
affected or retrieved. Zero indicates that no records were
updated for an UPDATE
statement, no rows
matched the WHERE
clause in the query or
that no query has yet been executed. -1 indicates that the
query returned an error or that, for a
SELECT
query,
mysql_affected_rows()
was called prior to
calling mysql_store_result()
. Because
mysql_affected_rows()
returns an unsigned
value, you can check for -1 by comparing the return value to
(my_ulonglong)-1
(or to
(my_ulonglong)~0
, which is equivalent).
Errors
None.
Example
mysql_query(&mysql,"UPDATE products SET cost=cost*1.25 WHERE group=10");
printf("%ld products updated",(long) mysql_affected_rows(&mysql));
If you specify the flag CLIENT_FOUND_ROWS
when connecting to mysqld,
mysql_affected_rows()
returns the number of
rows matched by the WHERE
statement for
UPDATE
statements.
Note that when you use a REPLACE
command,
mysql_affected_rows()
returns 2 if the new
row replaced an old row, because in this case, one row was
inserted after the duplicate was deleted.
If you use INSERT ... ON DUPLICATE KEY
UPDATE
to insert a row,
mysql_affected_rows()
returns 1 if the row
is inserted as a new row and 2 if an existing row is updated.