在postgreSQL中运行查询时出现SQL错误42601

时间:2017-05-17 06:48:23

标签: php mysql sql postgresql pdo

我正在尝试使用PHP PDO库针对pgSQL数据库表运行以下查询

SELECT COUNT(*) AS overall, COUNT(IF(username = ?, TRUE, NULL)) AS unames FROM login_attempt WHERE last_checked > NOW() - INTERVAL ? MICROSECOND

但得到以下错误

  

错误:SQLSTATE [42601]:语法错误:7错误:语法错误在或附近" $ 2"第1行:... gin_attempt WHERE last_checked> NOW() - INTERVAL $ 2 MICROSE ...... ^

这是我的完整功能代码

private function isQueueSizeExceeded()
{

    $sql = "SELECT COUNT(*) AS overall, COUNT(IF(username = :uname, TRUE, NULL)) AS unames FROM login_attempt WHERE last_checked > NOW() - INTERVAL :itime MICROSECOND";
    $stmt = $this->pdo->prepare($sql);

    $itime= 5000 * 1000;
    $stmt->bindParam(':uname', $this->username);
    $stmt->bindParam(':itime', $itime);
    $stmt->execute();

    $count = $stmt->fetch(PDO::FETCH_OBJ);
    if (!$count) {
        throw new Exception("Failed to query queue size", 500);
    }

    return ($count->overall >= self::MAX_OVERALL || $count->unames >= self::MAX_PER_USER);
}

此代码预计将使用MySQL运行,但我尝试使用postgreSQL

运行

1 个答案:

答案 0 :(得分:0)

DATATYPE 'string'语法仅适用于字符串文字,MICROSECONDS必须是字符串的一部分。

您可以使用类型转换:

... current_timestamp - CAST((:itime || ' microseconds') AS interval)