rubing Posted December 22, 2008 Share Posted December 22, 2008 I am running a current mysql 5 version on ubuntu using default myisam storage engine. I am logging page views for a file by querying for the number of times it has been already viewed, then adding one with php before updating. Is it ok to do it this way? $query="SELECT num_views FROM table1"; $result = $conn->query($query); if (($row = $result->fetch_assoc()) !== NULL) { $new_num_views=$row['num_views'] + 1; $query="UPDATE table1 SET num_views = $new_num_views"; $result=$conn->query($query); } Will this give me problems if two users request a page simultaneously? Will one of the views not be recorded?? Quote Link to comment https://forums.phpfreaks.com/topic/137967-solved-concurrent-update-after-select-potential-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 22, 2008 Share Posted December 22, 2008 Yes it is possible that concurrent execution of that code will loose some counts. Just execute a single query. There is no need to execute a query just to retrieve the value in order to add one to it - $query="UPDATE table1 SET num_views = num_views + 1"; Quote Link to comment https://forums.phpfreaks.com/topic/137967-solved-concurrent-update-after-select-potential-problem/#findComment-721124 Share on other sites More sharing options...
aschk Posted December 22, 2008 Share Posted December 22, 2008 I should point out that the query you are running will update EVERY row in your table, adding 1 to the num_views column for all the rows. I hope you're planning on sticking a WHERE clause on that to make it only update 1 row. Quote Link to comment https://forums.phpfreaks.com/topic/137967-solved-concurrent-update-after-select-potential-problem/#findComment-721344 Share on other sites More sharing options...
rubing Posted December 22, 2008 Author Share Posted December 22, 2008 Thanks for the fast response...it works great. I thought had tried that originally...guess not. Anyways, of course i have a where clause in the real query, but GOOD CATCH!! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/137967-solved-concurrent-update-after-select-potential-problem/#findComment-721413 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.