PDO - 更新查询的语法无效


PDO - Invalid syntax for UPDATE query

当我尝试执行以下查询时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 时必须有不同的方法来执行此操作。

你缺少一个集合

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

SET

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