Home » Programming » How to Run Multiple MySQL Queries with PHP Using PHP to Process More than One SQL Statement at the Same Time

RSS Fardin in Bremen

  • 19th century Market on the Grand Place of Brussels June 14, 2009
    by: Ibu ClaireThe main market of Brussels has been used as a market, since its creation beginning 10th century.Vegetables and poultry were sold, but also birds maintained into cages for their singing...At the end of 17th century, King Louis the 14th of France willing to conquer the Low Countries(name of Belgium at that time), bombed […]
  • Manneken-Pis June 14, 2009
    by : Ibu ClaireManneken-Pis (a 100m. away from the Grand-Place)This little 17th C. boy is supposed to be the oldest inhabitant of the city of Brussels. He is supposed to have gotten lost, and when found back, he was in this positon !His family was so happy that they offered a water-fontain to the inhabitants […]
  • Place Royale Brussels May 30, 2009
    by: Ibu ClaireThis square was built on the ruins of a previous royal palace, that burned out around 1715. Around 1780, huge works started in the "classical" style inspired by the castle of Versailles (France). It was supposed to be used for military parades,as the new palace was built "around the corner"Archeological researches were made […]
  • Atomium of Brussels 1958 May 21, 2009
    by: Ibu ClaireWas erected in 1958 for the World Exhebition. It represents a molecule of cristal iron and symbolizes the 9 provinces of Belgium. Each sphere has a diameter of 18 m.It is 102m. high and weighs 2400 tons. The designer was Ingenior André Waterkeyn.For its 5Oth anniversary, it was completely restaured,with partly by private […]

RSS Laravel

  • Short Course on Leisure – Getting to Square 1
    Laravel Short Course on Leisure – Getting to Square 1 Advantages of Movie Streaming The odds of you been disappointed after visiting a movie renting store and cinema are high if you love watching movies. The good news is that you do not have to worry anymore. You can now watch your favorite movie at […]
  • A Simple Plan: Scanners
    Laravel A Simple Plan: Scanners Choosing A Barcode Reader Because information technology is able to help a lot of processes, its use has become really known to the people. They have been able to ease many processes that in the past would have taken time. Because of the different areas that they are able to […]
  • Where To Start with Rings and More
    Laravel Where To Start with Rings and More Advantages of Buying Fake Diamond Rings Diamond have retained their value for many years which has made them be regarded as one of the expensive items. A lot of people buy these precious stones to express their love. Those who buy diamonds buy them in the form […]
  • Why Transcripts Aren’t As Bad As You Think
    Laravel Why Transcripts Aren’t As Bad As You Think The Amazing Advantages of Having Access to a Fake High School Diploma. The fake high school diplomas are actually provided out there. There are a number of reasons that explain why someone would actually want to have access to fake documents. There are actually innocent reasons […]

How to Run Multiple MySQL Queries with PHP Using PHP to Process More than One SQL Statement at the Same Time

by: Mark Alexander Bain

It’s very easy to send SQL queries to a MYSQL database and process the results (see How to Run MySQL Queries with PHP). However, running a number of queries one after another can be time consuming and memory intensive.

Fortunately PHP enables the programmer to run a number of queries at the same time and then process the returned results one record set at a time. The PHP programmer does this by using PHP’s mysqli object and its multi_query method.

Sending Multiple Queries to MySQL from PHP

Rather than repeatedly sending queries to the database it is possible to send a number of them at the same time. The first step, as always, is to instantiate the mysqli object:

$user = “db_user”;
$user_password = “db_password”;
$db_name = “db_demo”;
$db = new mysqli (“localhost”, $user, $user_password, $db_name);

With the connection in place the next stage is to create a string consisting of several SQL statements (remembering to separate each of them with a semi-colon):

$sql = “insert into articles
(title, url)
(‘How to Use the PHP Text Database API ‘,
$sql .= “insert into articles
(title, url)
(‘Using a PHP Session’,
$sql .= “select * from articles”;

Then the multi_query method sends the query to the database:

if ($db->multi_query($sql)) {

The PHP programmer will then need to process the results returned from the multiple queries. This is, of course, slightly more involved than handling a single query because more than one record set may be returned.

Processing the Results from Multiple Queries

When the PHP programmer processes the results from their queries they must:

  • Step though the record sets by using the next_result method
  • Load the current record set by using the store_result method

The end result is something like:

echo “<table width=100%>”;
while ($db->next_result()) {
if ($resultset = $db->store_result()) {
while ($record = $resultset->fetch_array(MYSQLI_BOTH)) {
echo “<tr>
<td>” . $record[‘title’] . “</td>
<td>” . $record[2] . “</td>
echo “</table>”;
} else {
echo $db->error . “<br>”;

Here only one recordset will be returned and processed (since only a single select statement is being used).


The PHP programmer sends queries to a MySQL programmer by creating a mysqli object. This object connects to the database and then accepts a single SQL statement via the query method. Multiple SQL statements can be sent to the database but this time programmer must use the multi_query method. The result of each query depends on the type of query sent.

Only select statements return a set of records. All queries return true or false depending on the query’s success or failure. Once the PHP application has received the record set then the results can be processed and presented to the web page user or used elsewhere in the application itself.

The copyright of the article How to Run Multiple MySQL Queries with PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish How to Run Multiple MySQL Queries with PHP in print or online must be granted by the author in writing.

RSS Geospatial

  • How to Achieve Maximum Success with Services
    Geospatial How to Achieve Maximum Success with Services The Benefits of Attending a Yoga Retreat A yoga retreat is a perfect option that you may consider when you are planning to go out on a holiday. With a yoga retreat, you get to benefit immensely. You will have an opportunity to relax your body and […]
  • The Path To Finding Better Health
    Geospatial The Path To Finding Better Health The Best Alcohol Rehab Center Any form of addiction, whether it is due to drugs or alcohol, can be treated. There are plenty of ways on how to get the right treatment. One of them is to find the best rehabilitation center. However, your search for the best […]
  • Learning The Secrets About Warranties
    Geospatial Learning The Secrets About Warranties Aspects That You Should Consider When Picking Protection Plan Services If you wish to enjoy the services of gadgets such as smartphones you need to ensure that they are in good shape. This cannot be achieved if they are not well protected from damage. It is your responsibility to […]
  • How I Became An Expert on Resources
    Geospatial How I Became An Expert on Resources Hints on Buying Bathmates Water based pumps are devices for enlarging the male organs. Males consider their organs to be beneficial as part of their body. The male organ has two main functions; reproduction and excretion activity. The waste in the form of urine gets out of […]