02 Votes

MySQL: Multiple queries simultaneously in PHP and phpMyAdmin

Question by Sledge | 2012-09-04 at 09:12

I want to run multiple MySQL commands at once. So far, I have done that with phpMyAdmin and separately for each statement with a semicolon. Like this:

UPDATE tab SET age=24 WHERE id=1;
UPDATE tab SET age=68 WHERE id=2;

Now I want to execute the same command in PHP and I have been thinking of the following solution:

$query = "UPDATE tab SET age=24 WHERE id=1;
          UPDATE tab SET age=68 WHERE id=2;";
 
mysql_db_query($database, $query, $connection);

Unfortunately, this leads always to an error. It is not wanting to work! And I don't know why. The same MySQL command is still working also in phpMyAdmin! Why would it suddenly stop working in PHP?

ReplyPositiveNegative
24 Votes

The PHP function mysql_connect always accept only a single statement. Here, you can not pass multiple statements separated by a semicolon, as it is the case in phpMyAdmin. To execute your two statements, you have to run mysql_db_query repeatedly.

Also phpMyAdmin uses no other version of PHP or magic in the performance of queries. Here, before performing, it is looked up, if there is a series of statements or not. If so, all MySQL commands are executed individually.
2012-09-05 at 00:43

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.