Victor
Victor

Reputation: 1013

mysql_affected_rows returns 0 but the database has changed

I am experiencing a strange behavior with the function mysql_affected_rows that returns 0 even if a register in the database is changed.

I have reduced the problem to the simplest possible thing, so you can easily reproduce it.

In the one hand I have one file (page1.php) with the following code that simply updates a value in a register of the database:

<?php
mysql_connect('localhost', 'dbu', 'pass');
mysql_select_db('db'); 
$stmt="UPDATE aa SET a=4 WHERE id=0";
echo $stmt;
mysql_query($stmt);
$n = mysql_affected_rows();
echo "<br>N:".$n;
?>

In the other hand I have another page (page2.php) with this arbitrarily unrelated code:

<?php
echo "HELLO";
?>

If I go to the page1, the database is updated, N is 1, everything is ok.

If I change the UPDATE statement to SET a=5 and then reload the page, database updated, N is 1, fine.

But, if I change the UPDATE to SET a=6, then I go to page2, and then I return to page1, then again the database is correctly updated BUT in this case N is 0. Why is mysql_affected_rows returning 0 only in this case?

Upvotes: 0

Views: 199

Answers (1)

SilicaGel
SilicaGel

Reputation: 459

mysql_affected_rows only works for the query that just ran in the executing script (If I recall correctly.) Once you go to page 2, then reload page 1, no changes are being made, since they were made the first time around.

Upvotes: 2

Related Questions