简体   繁体   中英

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?

mysql_affected_rows仅适用于刚刚在执行脚本中运行的查询(如果我没记错的话。)一旦转到第2页,然后重新加载第1页,则不会进行任何更改,因为它们是第一次更改。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM