PHP: multiple SQL queries in one mysql_query statement

You have more problem cases than just semicolons within strings.

  • Script builtin commands that cannot be executed by mysql_query(), like USE.
  • Statements that are not terminated with a semicolon, like DELIMITER.
  • Statements that contain semicolons, but not inside quotes, like CREATE PROCEDURE.

I don’t know of an easy way to handle this task, without shelling out to the mysql command-line client. I realize you said you can’t rely on that client being present, but without that client, you need a large amount of PHP code to parse the script and execute statements appropriately.

You may be able to find such code inside the phpMyAdmin product. However, that product is licensed under the GPL, so if you use any of the code, you must also license your own project under the GPL.


See also my answers to these related questions:

Leave a Comment