duanpen9294 2012-09-07 06:50
浏览 77
已采纳

mySQL Query无法在PHP中运行,但可以在PHPMyAdmin中运行

When I run an SQL query from PHP MyAdmin, it inserts the line correctly; when I run it from PHP, it does not.

I am going to provide you with the text in each; they are identical but given I might have missed something, I don't want to assume there are no differences;

IN 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);

Now in 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')

NOTE: I ran the PHP query WITHOUT the SET calls (the 3 lines setting the variables) and it worked; but I can't figure out where there is wrong syntax in those lines. Also ran mysql_error and it states that there is a syntax error - then asks me to look at the manual. Very helpful, mysql.

  • 写回答

2条回答 默认 最新

  • dtufl26404 2012-09-07 06:52
    关注

    mysql_query() cannot execute multiple queries at once.

    Two solutions:

    1. Use multiple mysql_query lines
    2. use mysqli with multi_query()

    Your code, rewriten to multiple mysql_query lines:

    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条)

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?