PDO - UPDATE查询的语法无效

时间:2012-05-25 20:02:25

标签: php sql pdo

当我尝试执行以下查询时,我收到错误SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= time + '1' WHERE username = 'admin-test'' at line 1

try
{
    $sth = $dbh->prepare("UPDATE alltimehighscores time = time + :time
        WHERE username = :username");
    $arr = array(
        ':username' => $username,
        ':time' => $time
        );
    $sth->execute($arr);
}
catch (PDOException $e)
{
    echo $e->getMessage();
    exit();
}

早先从$time分配$username$_GET值。上面也分配了$dbh,这是正常工作,因为上面有另一个查询执行正常。

查看错误消息我可以看到time没有被更改为当前数据库值,所以我假设在使用PDO时必须有不同的方法。

2 个答案:

答案 0 :(得分:6)

你错过了一个SET

UPDATE alltimehighscores SET time = time + :time WHERE username = :username

答案 1 :(得分:1)

SET缺失:

UPDATE alltimehighscores SET `time` = `time` + :time
WHERE username = :username