开发者

Strange behavior of MySQL UPDATE query in PHP?

开发者 https://www.devze.com 2022-12-24 11:38 出处:网络
When I am executing following query then its not updating views column by 1 instead sometimes its updating it by 2 or 3. Say currently views count is 24 then after executing this query it becomes 26 o

When I am executing following query then its not updating views column by 1 instead sometimes its updating it by 2 or 3. Say currently views count is 24 then after executing this query it becomes 26 or sometimes its 27.

$views = $views + 1;
$_SQL = '';
$_SQL = 'UPDATE videos SET views = '.$views.' WHERE VideoId= "'.$videoid.'";';
@mysql_query($_SQL);

I am not getting why this is happening, am I missing something or开发者_如何学JAVA the query is executing 2 times automatically? Please help me to figure out the issue.

Thanks


Instead of storing the view count, just have the SQL query increment the views,

 $_SQL = 'UPDATE videos SET views = views + 1 WHERE VideoId= "'.$videoid.'";'


Is it possibly within a loop? The query is only executing once with this code posted however is this code contained within a while/for loop? If it is check it and then just move the

$views = $views + 1;

outside the scope of the loop.


You could try printing the value of $views after incrementing it just to see if the problem is in the PHP code or in the SQL query.

0

精彩评论

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