my_ulonglong mysql_affected_rows(MYSQL
*mysql)
Description
After executing a statement with
mysql_query()
or
mysql_real_query()
, returns the
number of rows changed (for
UPDATE
), deleted (for
DELETE
), or inserted (for
INSERT
). 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
char *stmt = "UPDATE products SET cost=cost*1.25 WHERE group=10"; mysql_query(&mysql,stmt); printf("%ld products updated", (long) mysql_affected_rows(&mysql));
For UPDATE
statements, if you
specify the CLIENT_FOUND_ROWS
flag when
connecting to mysqld,
mysql_affected_rows()
returns
the number of rows matched by the WHERE
clause. Otherwise, the default behavior is to return the number
of rows actually changed.
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.
User Comments
Add your own comment.