Jump to content

[SOLVED] concurrent update after select potential problem


rubing

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.