开发者

mySQL table updates, but not fast enough for a select statement on the same page to access the updates - any ideas?

开发者 https://www.devze.com 2023-02-12 01:37 出处:网络
I am trying to write some code that updates a mysql table, and then selects out of that same table in the same page.However, I find that when I do the update query, then the select query, it does not

I am trying to write some code that updates a mysql table, and then selects out of that same table in the same page. However, I find that when I do the update query, then the select query, it does not recognize the changes. If, however, I refresh the page, then it reco开发者_Go百科gnized the changes.

I first have an insert statement something like this

$query = 'INSERT INTO matches (uid, win) VALUES ($uid, $win)';

mysql_query($query) or die(mysql_error() . ' in ' . $query);

Then, just after this, I have a select statement like

$query = "SELECT * FROM matches where uid = $uid";

$resultmain = mysql_query($query) or die(mysql_error() . ' in ' . $query);

Of course I simplified the queries but, that is the general idea - and what happens is: the select statement will not recognize the update that was run immediately before it. However, if I reload the page, and the select statement runs again after some time, it does recognize the change.

I googled for this and was very surprised to not come across anything yet. Is there any good way to force to wait until the mysql update query finished before selecting? If not, I might just have to use javascript to automatically reload the page, but this sounds like a messy solution.

Any help would be greatly appreciated, this has been driving me crazy...

--Anthony


That should not happen. Maybe it’s a problem in your code, which you did not post?

Things that come to mine, which could be the problem:

The 2 queries are run on different connections to the MySQL database. And auto-commit is not enabled.

Thus, first query would send the update but not commit, the second query will query on old data, and only after the page finishes (/later on) the commit occurs.

I’m not quite sure if non-auto-commited changes will be commited or rolled back when a PHP script ends, but it should be a rollback. Thus a later commit would be needed in your code as well for this possible scenario to apply.

0

精彩评论

暂无评论...
验证码 换一张
取 消