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

  • If You Read One Article About , Read This One
    Laravel If You Read One Article About , Read This One Benefits of Choosing the Best Funeral Home Losing a family member is really tough. Immediate family and friends are affected by the loss. unexpected expenses come up when bereaved. Choosing the right funeral home can be quite beneficial. Having the burial planned for you […]
  • The 10 Laws of And How Learn More
    Laravel The 10 Laws of And How Learn More How to Identify the Best Wedding Dress Store As you will be preparing for the day you will get married to the love of your life, you will have to make sure that you will acquire the best gown for your d day. This is by […]
  • The Best Advice About I’ve Ever Written
    Laravel The Best Advice About I’ve Ever Written Psychoanalysis to Understand Post Traumatic Experiences You may have had a really traumatic experience at one point in your life but what that thing happened, you might have not given it much thought or even forget about it as soon as it passed. The next year, you […]
  • Why Aren’t As Bad As You Think
    Laravel Why Aren’t As Bad As You Think Why Selling Your FedEx Routes Benefits You? In every turn and take there is a reason. And you sure have been hearing about people selling their routes, but quite honestly you haven’t thought about one day you will be in need of such information because you too […]

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)
values
(‘How to Use the PHP Text Database API ‘,
‘http://www.suite101.com/functions/article/edit.cfm/103536’);”;
$sql .= “insert into articles
(title, url)
values
(‘Using a PHP Session’,
‘http://www.suite101.com/functions/article/edit.cfm/102893’);”;
$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>
</tr>”;
}
$resultset->free();
}
}
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).

Summary

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

  • 3 Tips from Someone With Experience
    Geospatial 3 Tips from Someone With Experience Vital Information to Know About Bottled Water Water is essential to the life of all living organisms; thus, the term water is life. Water is used for different purposes in industrial purposes as raw material, for cleaning products, for cooling machines and as a reagent. Water is also […]
  • Smart Ideas: Revisited
    Geospatial Smart Ideas: Revisited Factors to Consider When Choosing a Dental Clinic Center What is a dental clinic? A dental clinic is a health Centre where oral services are rendered. People are advised to visit the dental clinic regularly so that they can ensure oral hygiene health which is a very important part of the […]
  • What Almost No One Knows About
    Geospatial What Almost No One Knows About Tips to Look into When Hiring Janitorial Services Cleanliness is a significant point that needs to be adhered be it in a residential or commercial place. To job to be done perfectly, then it is relevant for an individual to hire the services of professionals. Due to the […]
  • 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 […]