我正在尝试在 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-&gt; execute(Array)#1 /home/integra/public_html/index.php(124):include('/ home / integra / p ......')第48行/home/integra/public_html/service_status/index.php中的#2 {main}
答案 0 :(得分:2)
你正在传递:
':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)