views:

362

answers:

2

mysql_affected_rows is to get number of affected rows in previous MySQL operation, but I want to get affected rows in previous MySQL operation. For example:

update mytable set status=2 where column3="a_variable";

Before this operation, status of some rows is already 2, and I want to get affected rows in previous MySQL operation, you can not get it by issuing a query of

select * from mytable where status=2

So how to do this work?

A: 

The implementation and function you are asking about is from PHP, please tag your question with PHP.

In response to your question tho, in order to do what you are suggesting you would need to keep a copy of the SQL table prior to the change and compare it with the table after the change, the resulting difference would be your answer. You can do this in PHP by simply loading the table contents into an array and comparing the arrays before and after, any new/changed data is your answer (Please note: it is highly not recommended you do this as it can cause an increased load on your server depending on the amount of data stored in the tables.)

Zyris Development Team
+1  A: 

Hi, It can be efficiently and simply achieved with the following:

 select * from mytable where column3="a_variable" and status != 2;
 update mytable set status=2 where column3="a_variable";

The result of the first query are the rows that are going to change, and the second query actually changes them.

If this is a high performance system, you may need to take special care with transactions to prevent a new row slipping in between those 2 queries.

carl