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

  • Practical and Helpful Tips:
    Laravel Practical and Helpful Tips: Picking the Most Excellent Drywall Repair Company The drywall is an integral component of your house because it provides some insulation, which leads to high energy efficiency. Besides, you can improve the beauty of your home when you maintain the drywall in the right way. The drywall might destruct after […]
  • The 10 Best Resources For
    Laravel The 10 Best Resources For Tips to Contemplate When Finding the Unsurpassed Occupational Therapist When people have issues when it comes to movement, then you ought to contemplate on finding the solution. Occupational therapy can help in improving your movement, which means that you should search for occupational therapy services. However, you can find […]
  • Questions About You Must Know the Answers To
    Laravel Questions About You Must Know the Answers To The Benefits of Minnesota Resorts Vacations are amazing as they allow for one to have a great time where they are away from their day to day life. This means that one gets to be relaxed and not be stressed over work as you are relaxed […]
  • The 10 Best Resources For
    Laravel The 10 Best Resources For Here Are Considerations to Make When Selecting a Pilates Studio A majority of individuals have never heard of Pilates methodology in the past, yet many studios have been offering these techniques for decades. Just recently that many people tend to opt for it as it has proven to provide […]

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

  • 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 […]