Name
mysql_affected_rows —
Synopsis
my_ulonglong mysql_affected_rows(MYSQL *mysql)
Returns
the number of rows affected by the most recent query. When used with
a non- SELECT
query, it can be used after the
mysql_query
call that sent the query. With
SELECT
, this function is identical to
mysql_num_rows
. This function returns 0, as
expected, for queries that affect or return no rows. In the case of
an error, the function returns -1.
When an UPDATE
query causes no change in the value
of a row, that row is not usually considered to be affected. However,
if the CLIENT_FOUND_ROWS
flag is set when
connecting to the MySQL server (see the
mysql_real_connect
function), any rows that match
the WHERE
clause of the UPDATE
query are considered affected.
Example
/* Insert a row into the people table */ mysql_query(&mysql, "INSERT INTO people VALUES ('', 'Illyana Rasputin', 16)"; num = mysql_affected_rows(&mysql); /* num should be 1 if the INSERT (of a single row) was successful, and -1 if there was an error */ /* Make any of 'HR', 'hr', 'Hr', or 'hR' into 'HR'. This is an easy way to force a consistent capitalization in a field. mysql_query(&mysql, "UPDATE people SET dept = 'HR' WHERE dept LIKE 'HR'"); affected = mysql_affected_rows(&mysql); /* By default, 'affected' will contain the number of rows that were changed. That is, the number of rows that had a dept value of 'hr', 'Hr' or 'hR'. If the CLIENT_FOUND_ROWS flag was used, 'affected' will contain the number of rows that matched the where. ...
Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.