first come first serve php
I am a newbie in php and working on a webservice to increase itemQuantity from iphone end. When the user hits the + button to开发者_JAVA百科 increase quantity of an item multiple times , then it some times does not accurately increase the quantity. iphone is sending me increased quantity everytime. I mean 26 then 27 then 28.
But when I tried to debugged it I found some time it firstly serve the database with 28 then update it to 27 . can anybody please help ? function I am using is as following....
function update_query($tblName,$queryArr,$condition)
{
if(!empty($this->_database)){
if(!mysql_select_db($this->_database, $this->_link) )
{
$this->_errorstr = "Couldn't change database: ".$this->_database."
My-SQL Error ".mysql_error($this->_link);
}
}
$fields=@array_keys($queryArr);
$values=@array_values($queryArr);
$update="";
for($i=0;$i<count($values);$i++)
{
$update.="$fields[$i]='".addslashes($values[$i])."',";
}
$update=substr($update,0,-1);
$query="update $tblName set $update $condition";
// echo $query
//echo $query."<br>";
//die;
$this->_errorstr = "";
$this->_query = $query;
$this->_result = mysql_query( $this->_query, $this->_link );
if ( ! $this->_result )
{
$this->_errorstr = "Error : ".mysql_error( $this->_link );
$this->_success = false;
}
else
{
$this->_success = true;
}
return $this->_success;
}
What happens if two requests arrive at the same time to your application and both read the same value from the db?
Use SET quantity = quantity + 1
in you mysql query instead.
This does not solve the display problem in your iphone application, but at least, the users command has been stored inside the data.
To solve the display problem, you need to change the design of your application. For example, increase on client side and store later on.
精彩评论