我的 php 脚本中有一个由 PDO 处理的查询。确切地说,它是 1 个语句中的 2 个查询。查询本身在 SQL 客户端中运行良好(我使用的是 HeidiSQL)。然而 PHP 却给了我这个错误:“ SQLSTATE[HY000]: General error ”,没有别的。没有错误号或消息。
有没有办法以某种方式调试查询?我认为查询本身没有错误,所以我不知道出了什么问题。mysql错误日志中没有显示任何内容。我启用了 mysql 常规日志,但它只记录查询本身而不显示错误。
我的堆栈:XAMPP 1.8、Apache 2.4.3、PHP 5.4.7、MySQL 5.5.27
这是我的查询(相当长):
/*First query - generating temp table with overdue jobs*/
CREATE TEMPORARY TABLE temp AS (
SELECT j.NetworkID,
@clientID := j.ClientID,
j.BranchID,
j.ServiceProviderID,
(
(DATEDIFF(CURDATE(), j.DateBooked))
-
IF(
(@unit := (
SELECT uctype.UnitTurnaroundTime
FROM job
LEFT JOIN product ON job.ProductID = product.ProductID
LEFT JOIN unit_type AS utype ON product.UnitTypeID = utype.UnitTypeID
LEFT JOIN unit_client_type AS uctype
ON utype.UnitTypeID = uctype.UnitTypeID
AND uctype.ClientID = @clientID
WHERE job.JobID = j.JobID
)
) IS NOT NULL, /*statement*/
@unit, /*TRUE - Client Unit Type has turnaround time assigned in the db*/
IF( /*FALSE - Now checking if Client Default Turnaround Time is set*/
(@clnt := (
SELECT DefaultTurnaroundTime AS dtt
FROM client
WHERE client.ClientID = @clientID
)
) IS NOT NULL, /*statement*/
@clnt, /*TRUE - Client Default Turaround time is set*/
( /*FALSE - falling back to general default*/
SELECT gen.Default
FROM general_default AS gen
WHERE gen.GeneralDefaultID = 1
)
)
)
) AS overdue
FROM job AS j
HAVING overdue > 0
);
/*Second query - filtering out overdue jobs with specific time range*/
SELECT COUNT(*) AS number
FROM temp
WHERE overdue >= :from AND overdue <= :to AND overdue != 0
更新:问题似乎是由 PDO 拒绝在一个语句中运行多个查询引起的。我在执行查询之前插入了这个:
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
所以现在“一般错误”消失了,但我得到了
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax
就在第一个查询之后,这意味着 PDO 只运行一个查询并拒绝继续......