我有以下查询返回特定客户的结果:
$enviroFigures = $dm->createQuery("
SELECT efu.wasteType, efu.ewcCode, SUM(efu.totalUom) AS totalTonne, SUM(efu.co2Saving) AS totalCO2
FROM CoreBundle:EnviroFiguresUpload efu
WHERE efu.customerSite = :site
AND efu.completionDate BETWEEN :start AND :end
AND efu.completionDate != :blank
AND efu.totalUom != :blank
GROUP BY efu.wasteType
ORDER BY efu.completionDate DESC"
)->setParameters(array(
"site" => $selectTerm,
"start" => $ds,
"end" => $de,
"blank" => ""
));
$enviFig = $enviroFigures->getResult();
$selectTerm
可以包含撇号(例如TGI Friday's
),但这会导致PHP中的错误,因为撇号会关闭查询。我怎么能避免这个?我无法从变量中删除撇号。
修改
这是日志中的错误条目。
[2015-01-11 11:31:42] request.CRITICAL:未捕获PHP异常 Doctrine \ DBAL \ DBALException:“执行时发生异常 'SELECT SUM(total_uom)AS totalTonne,waste_hierarchy,completion_date FROM enviro_figures_upload WHERE customer_reference ='TGI Friday's 格拉斯哥'AND completion_date BETWEEN'2014-01-11'和'1970-01-01'AND completion_date!=''AND waste_hierarchy!=''GROUP BY waste_hierarchy,DATE_FORMAT(completion_date,'%m-%Y')DESC ORDER BY YEAR(completion_date)ASC,MONTH(completion_date)ASC,waste_hierarchy ASC':SQLSTATE [42000]:语法错误或访问冲突:1064你有 SQL语法中的错误;查看与您的手册相对应的手册 MySQL服务器版本,用于在格拉斯哥附近使用正确的语法 completion_date BETWEEN'2014-01-11'and'1970-01-01'AND complet'at 第1行“at /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php line 91 {“exception”:“[object](Doctrine \ DBAL \ DBALException:异常 执行'SELECT SUM(total_uom)AS totalTonne时发生, waste_hierarchy,completion_date FROM enviro_figures_upload WHERE customer_reference ='TGI Friday's Glasgow'和completion_date '2014-01-11'和'1970-01-01'之间的关系和完成日期!=''和 waste_hierarchy!=''GROUP BY waste_hierarchy, DATE_FORMAT(completion_date,'%m-%Y')DESC ORDER BY YEAR(completion_date)ASC,MONTH(completion_date)ASC,waste_hierarchy ASC':\ n \ nSQLSTATE [42000]:语法错误或访问冲突:1064您 您的SQL语法有错误;检查对应的手册 您的MySQL服务器版本,以便在格拉斯哥附近使用正确的语法 AND completion_date BETWEEN'2014-01-11'AND'1970-01-01'AND complet' 在第1行 /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:91, PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064 您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 '格拉斯哥附近'和完成日期'2014年2月11日'和 '1970-01-01'AND complet'在第1行 /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:165)“}