How to Send Multiple Queries to the MySQL Server - dummies

How to Send Multiple Queries to the MySQL Server

By Steve Suehring, Janet Valade

Sometimes, you want to send two or more queries to the MySQL server at the same time. MySQL allows you to do so, but you need to use a different function to send the queries. You can send multiple queries with the following function:


You send the queries in a single string with the queries separated by a semicolon:

$query = "SELECT * FROM Cust;SELECT * FROM OldCust";

The multiple_query function isn’t available with the mysql functions, only with the mysqli functions.

Sending queries can be less secure than sending one query. Whenever you use data from an outside source, be sure you validate the outside data thoroughly. For instance, suppose you display a form asking the user for a table name, and you create a query from the table name that the user enters, as follows:

$query = "SELECT * FROM Friend";

The user enters the table name Friend. The query is fine. However, suppose the user enters the following into the form:

Friend;DELETE TABLE Friend

Your query then is

$query = "SELECT * FROM Friend;DELETE TABLE Friend";

If you send this query, the query is not so fine. You won’t like the results. You probably didn’t want the table deleted. Be sure to always sanitize data before sending it to MySQL!