Keeping Up with PHP and MySQL Changes
How to Connect the Top, Bottom, and Middle of a PHP Template
How to Select a MySQL Database

How to Send Multiple Queries to the MySQL Server

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:

mysqli_multi_query($cxn,$query)

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

$query = "SELECT * FROM Cust;SELECT * FROM OldCust";
mysqli_multi_query($cxn,$query);

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!

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Choose the Data for Your MySQL Database
PHP & MySQL For Dummies Cheat Sheet
PHP Statements
Design a Sample MySQL Database
How to Use PHP Variable Variables
Advertisement

Inside Dummies.com