简体   繁体   English

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

[英]SQL error 42601 when running query in postgreSQL

I am trying to run following query using PHP PDO library against a pgSQL Database table我正在尝试使用 PHP PDO 库对 pgSQL 数据库表运行以下查询

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

but getting following error但出现以下错误

Error: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "$2" LINE 1: ...gin_attempt WHERE last_checked > NOW() - INTERVAL $2 MICROSE... ^错误:SQLSTATE[42601]:语法错误:7 错误:在“$2”或附近出现语法错误第 1 行:...gin_attempt WHERE last_checked > NOW() - INTERVAL $2 MICROSE... ^

here is my full code of function这是我的完整功能代码

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);
}

this code is expected to run using MySQL but i am trying to run using postgreSQL此代码预计将使用 MySQL 运行,但我正在尝试使用 postgreSQL 运行

The DATATYPE 'string' syntax only works for string literals, and the MICROSECONDS must be part of the string. DATATYPE 'string'语法仅适用于字符串文字, MICROSECONDS必须是字符串的一部分。

You could use a type cast instead:您可以改用类型转换:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM