MYSQL查询可以直接工作,但不能通过PHP工作!

时间:2011-03-13 07:24:37

标签: php mysql

我在PHP页面上弹出以下错误。这是在查询发生之前回显$ flightquery的结果。

INSERT INTO Flights (`adshex`,`flightno`,`route`) VALUES ('01002F','MSR845','HECA-DAAG') WHERE NOT EXISTS (SELECT flightno FROM Flights WHERE `flightno` = 'MSR845' AND `adshex` = '01002F' AND TIMESTAMPDIFF(MINUTE,`timestamp`,CURRENT_TIMESTAMP) > 360);

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS (SELECT flightno FROM Flights WHERE `flightno` = 'MSR845' AND `' at line 1

如果我将insert语句复制到MYSQL服务器中它没有错误,但是通过PHP它会中断!

  $flightquery = "INSERT INTO Flights (`adshex`,`flightno`,`route`) VALUES ('$adshex','$flightno','$route') WHERE NOT EXISTS (SELECT flightno FROM Flights WHERE `flightno` = '$flightno' AND `adshex` = '$adshex' AND TIMESTAMPDIFF(MINUTE,`timestamp`,CURRENT_TIMESTAMP) > 360);";

更新

我把它更改为errno,它是1064,这是保留关键字,我在做什么?时间戳?

3 个答案:

答案 0 :(得分:2)

让坏男孩在;后删除360)

答案 1 :(得分:2)

MySQL syntax for INSERT WHERE INSERT条款{{1}}。

答案 2 :(得分:0)

查询在MySQL服务器中不起作用,因为有PHP变量。

$ adshex,$ flightno,$ route

"INSERT INTO Flights (`adshex`,`flightno`,`route`) 
VALUES ('$adshex','$flightno','$route') 
WHERE NOT EXISTS (SELECT flightno FROM Flights 
    WHERE `flightno` = '$flightno' AND `adshex` = '$**adshex**' 
    AND TIMESTAMPDIFF(MINUTE,`timestamp`,CURRENT_TIMESTAMP) > 360);