1

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.

1

2 Answers 2

7

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')");
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Maris, I was initially going to do multiple queries but then thought: doesn't doing multiple queries slow down the process? I will have a user waiting for a confirmation the order has gone through (as you can see the script involves a menu) and so I thought running everything in a single Mysql query would be quicker; am I misunderstanding how MySQL works?
Of course, single group of queries will be a bit faster. You still tried to run many queries but in one request. I would rewrite your queries in to one single query. Check dev.mysql.com/doc/refman/5.0/en/insert-select.html
0

You have following options:

  1. Use multiple queries to retrieve values and and make final query.
  2. Use mysqli_query
  3. Make a stored procedure and call it. (Recommended, then you need not worry about escape characters)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.