我有這個PHP代碼查詢到MySQL數據庫:MySQL錯誤從PHP查詢:IF EXISTS
$query2 = "IF(EXISTS (SELECT * FROM shipcargo WHERE shipid='$shipid' AND item='$item' AND price='$price'))
BEGIN
UPDATE shipcargo SET amount = amount+'$amount' WHERE shipid='$shipid' AND item='$item' AND price='$price'
END
ELSE
BEGIN
INSERT INTO shipcargo (shipid, item, amount, price) VALUES('$shipid', '$item', '$amount', '$price')
END";
mysql_query($query2) or die(mysql_error());
返回的錯誤是:
您的SQL語法錯誤; (EXISTS(SELECT * FROM shipcargo WHERE shipid = '11'AND item ='WheatBastard'AN'at line 1)
PHP不會觸發SQL錯誤。你必須檢查'$ query2'包含的是否是有效的SQL代碼。 – 2012-03-05 11:05:05