MySQL查询在命令行中运行良好,但在php中运行时出错

时间:2013-10-01 12:05:00

标签: php mysql

我有一个查询,当我在我的linux服务器命令行上运行它时工作正常,但是当我尝试通过php执行i时,它会抛出一个php错误。

SELECT h.timestamp, d.valueparam, r.label, r.dataUnit 
FROM EventData d, EventHeader h, EventKeyReference r 
WHERE h.id = d.eventHeader_id AND h.serialNumber=$serialnumber 
AND d.keyParam='G002' AND r.EventKey = 'G002' 
ORDER BY h.timestamp DESC LIMIT 1;

它给了我这个错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'AND d.keyParam ='G002'AND r.EventKey ='G002'ORDER BY   h.timestamp DESC LIMIT 1'在第1行

有谁知道什么是错的?

3 个答案:

答案 0 :(得分:4)

我的猜测:$serialnumber为空,您的查询为:

SELECT h.timestamp, d.valueparam, r.label, r.dataUnit 
FROM EventData d, EventHeader h, EventKeyReference r 
WHERE h.id = d.eventHeader_id AND h.serialNumber=
                                               // ^-- error here
AND d.keyParam='G002' AND r.EventKey = 'G002' 
ORDER BY h.timestamp DESC LIMIT 1;

答案 1 :(得分:0)

如下所示更新您的查询,即使serialnumber为空也会有效

$sql = "SELECT h.timestamp, d.valueparam, r.label, r.dataUnit 
FROM EventData d, EventHeader h, EventKeyReference r 
WHERE h.id = d.eventHeader_id AND h.serialNumber='".$serialnumber."'
AND d.keyParam='G002' AND r.EventKey = 'G002' 
ORDER BY h.timestamp DESC LIMIT 1";

答案 2 :(得分:0)

更改

AND h.serialNumber=$serialnumber 

AND h.serialNumber='$serialnumber' 

这将捕获任何带有字母的空$ serialnumber或$ serialnumber。