【问题标题】:PDO Sql union query displays errorPDO Sql union 查询显示错误
【发布时间】:2014-01-28 07:20:16
【问题描述】:

我正在尝试在 PHP PDO 中运行此 SQL 查询:

$stmt = $pdo_conn->prepare("SELECT * from integra_status where type <> :type1 and category1 = :category and (status = :status1 or status = :status2) 
    UNION 
    SELECT * from integra_status WHERE type = :type2 and maintenance_fromdate <= :maintenance_fromdate AND maintenance_todate >= :maintenance_todate and category = :category2 order by sequence ASC ");
    $stmt->execute(array(':type1' => 'Maintenance', ':category1' => $result["sequence"], ':status1' => 'Open', ':status2' => 'Resolved', 
    ':type2' => 'Maintenance', ':maintenance_fromdate' => 'DATE_ADD(NOW(), INTERVAL 7 DAY)', ':maintenance_todate' => 'DATE_SUB(NOW(), INTERVAL 2 DAY)', ':category2' => $result["sequence"] ));

但我得到了这个输出:

致命错误:未捕获的异常 'PDOException' 和消息 'SQLSTATE[HY093]:无效的参数号:参数未定义'在 /home/integra/public_html/service_status/index.php:48 堆栈跟踪:# 0 /home/integra/public_html/service_status/index.php(48): PDOStatement->execute(Array) #1 /home/integra/public_html/index.php(124): include('/home/integra/p. ..') #2 {main} 在第 48 行的 /home/integra/public_html/service_status/index.php 中抛出

【问题讨论】:

    标签: php sql pdo


    【解决方案1】:

    你正在通过:

    ':category1' => $result["sequence"]
    

    在您的执行语句中,而您的查询显示:

    where type <> :type1 and category1 = :category and
    

    另请注意,您将无法使用

    ':maintenance_fromdate' => 'DATE_ADD(NOW(), INTERVAL 7 DAY)'
    

    如你所愿。直接在查询中使用该函数:

    maintenance_fromdate <= DATE_ADD(NOW(), INTERVAL 7 DAY)
    

    【讨论】:

      猜你喜欢
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      相关资源
      最近更新 更多