Jump to content

[SOLVED] concurrent update after select potential problem


Recommended Posts

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??

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";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.