mysql plus one a column



我使用我自己的MVC框架。

和post action是:

function post($pid = 0 , $title = '')
{
    $pid = $this->input->clean($pid);
    $stm =$this->Post->query("UPDATE posts SET `show_count`=show_count+1 WHERE id = $pid"); 
    $stm->execute();
    $row = $this->Post->find()->where(array('id'=>$pid))->fetchOne();
    $this->layout->set('comments' ,  $this->comments($pid));
    $this->layout->set('row' , $row);
    $this->side();
    $this->layout->view('post'); 
    echo $this->layout->render($row['title']);
}

我想当一个记录从数据库中取出加一个show_count列。

我使用这个查询:

UPDATE posts SET show_count = show_count + 1 WHERE id = $pid

在本地主机上,但在我的共享主机上运行查询而不是1 +,2 + show_count列。

如何解决这个问题?

它在你的本地主机工作,但不是你的互联网主机。主持人可能在幕后做了一些事情,使它无法工作。此外,由于您无法访问主机服务器上的所有配置内容,因此您可能无法直接修复它。

最简单的修复方法是在软件层进行(假设$pid是唯一的)。

首先获取show_count。

:

$oldShowCount = the current show_count
$newShowCount = $oldShowCount + 1
UPDATE posts SET show_count = $newShowCount WHERE id = $pid AND show_count = $oldShowCount

此时,如果没有竞争条件发生,将更新行(rows update = 1),如果有竞争条件发生,将更新失败(rows updated = 0)。

然后检查更新的# rows是否有效,并重复,直到它成功。

这是很常见的情况。您运行了两次脚本。可能是因为浏览器调用了它两次。检查你的重写规则

相关内容

最新更新