当我从PHP MyAdmin运行SQL查询时,它会正确插入行;当我从PHP运行它时,它没有。
我将为您提供每个文本;他们是完全相同的,但鉴于我可能错过了一些东西,我不想假设没有差异;
在PHP中:
$orderSend = "SET @typeSQL := (SELECT `typeID` FROM `ArbuckleType` WHERE `typeName` = 'A la Carte');
SET @groupSQL:= (SELECT `groupID` FROM `ArbuckleGroup` WHERE (`groupName` = 'Nigiri' AND `typeID` = @typeSQL));
SET @itemSQL:=(SELECT `itemID` FROM `ArbuckleItem` WHERE (`itemName` = 'Salmon' AND `groupID` = @groupSQL));
INSERT INTO `ArbuckleOrderDetails` VALUES('', '$orderID', @typeSQL,@groupSQL, @itemSQL, '$quantity', '$spicy')";
mysql_query($orderSend);
现在使用PHP MyAdmin:
SET @type := (SELECT `typeID` FROM `ArbuckleType` WHERE `typeName` = 'A la Carte');# MySQL returned an empty result set (i.e. zero rows).
SET @group:= (SELECT `groupID` FROM `ArbuckleGroup` WHERE (`groupName` = 'Nigiri' AND `typeID` = @type));# MySQL returned an empty result set (i.e. zero rows).
SET @item:=(SELECT `itemID` FROM `ArbuckleItem` WHERE (`itemName` = 'Salmon' AND `groupID` = @group));# MySQL returned an empty result set (i.e. zero rows).
INSERT INTO `ArbuckleOrderDetails` VALUES('', '$orderID', @type,@group, @item, '$quantity', '$spicy')
注意:我运行PHP查询没有SET调用(设置变量的3行)并且它工作;但我无法弄清楚这些行中的语法错误。还运行mysql_error并且它声明存在语法错误 - 然后要求我查看手册。非常有帮助,mysql。
答案 0 :(得分:7)
mysql_query()不能一次执行多个查询。
两种解决方案:
您的代码,重写为多个mysql_query行:
mysql_query("SET @typeSQL := (SELECT `typeID` FROM `ArbuckleType` WHERE `typeName` = 'A la Carte')");
mysql_query("SET @groupSQL:= (SELECT `groupID` FROM `ArbuckleGroup` WHERE (`groupName` = 'Nigiri' AND `typeID` = @typeSQL))");
mysql_query("SET @itemSQL:=(SELECT `itemID` FROM `ArbuckleItem` WHERE (`itemName` = 'Salmon' AND `groupID` = @groupSQL))");
mysql_query("INSERT INTO `ArbuckleOrderDetails` VALUES('', '$orderID', @typeSQL,@groupSQL, @itemSQL, '$quantity', '$spicy')");
答案 1 :(得分:0)
您有以下选项: