Bug #2247 mysql_stmt_affected_rows returns affected rows from last command
Submitted: 1 Jan 2004 6:35 Modified: 20 Feb 2004 2:15
Reporter: Georg Richter Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.1.2 OS:Any (any)
Assigned to: Konstantin Osipov CPU Architecture:Any

[1 Jan 2004 6:35] Georg Richter
Description:
I'm not sure if it's a documentation or implementation bug:

http://www.mysql.com/doc/en/mysql_stmt_affected_rows.html
Returns the total number of rows changed, deleted, or inserted by the last executed statement.

mysql_stmt_affected_rows returns stmt->mysql->last_used_conn->affected_rows which means it will change also when you execute a query, not only a prepared statement. If this is expected behaviour do we need an additional api function, when you can also retrieve this value with mysql_affected_rows ?

How to repeat:
...
stmt = mysql_prepare(mysql, "INSERT INTO foo VALUES (1),(2),(3)", 100);
mysql_execute(stmt);

/* should be 3 */
printf("Affected rows: %d\n", mysql_stmt_affected_rows(stmt));

mysql_query(mysql, "UPDATE foo SET a=7 WHERE a=1");

/* should be 3 but is 1 */
printf("Affected rows: %d\n", mysql_stmt_affected_rows(stmt));
..
[5 Jan 2004 15:47] Dean Ellis
Verifying, though this sounds more like a matter that needs internal discussion.
[5 Jan 2004 16:43] Konstantin Osipov
This is description of 2291 which is a copy of this bug:

Description: Implementation of mysql_stmt_affected_rows() is the following:
/*  
  Return total affected rows from the last statement
*/
  
my_ulonglong STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt)
{ 
  return stmt->mysql->last_used_con->affected_rows;
}
  

We should return statement-local variable here, otherwise that call is nothing
but usual mysql_affected_rows

How to repeat:
-

Suggested fix:
Add statement-local variable affected_rows and copy it from
mysql->last_used_conn->affected_rows in execute() function of  libmysql.c
[20 Feb 2004 2:15] Konstantin Osipov
Fixed in 4.1.2, bk commit - 4.1 tree (konstantin:1.1694)