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

  • Overwhelmed by the Complexity of ? This May Help
    Laravel Overwhelmed by the Complexity of ? This May Help What to Ask HVAC Contractor Now that you are the owner of your assets, you should show some obligations and have it repaired or maintained when supposed to. You should take your home to be an essential financial investment that you own in life. It […]
  • The Art of Mastering
    Laravel The Art of Mastering Considerations in Choosing the Best Orthodontist Have you been searching for an orthodontist and the search is proving futile? Relax if that is your case because in this article you will be able to learn more about orthodontists and what you ought to consider while choosing the best orthodontist. An […]
  • Learning The Secrets About
    Laravel Learning The Secrets About Features of a Good Website In case you need your brand to sell fast and in a smooth way, you will agree that using a good website is the solution if you are a business person who knows the importance of using internet as a way of marketing. When a […]
  • Why No One Talks About Anymore
    Laravel Why No One Talks About Anymore How To Create Your Own Website As A Beginner It is possible that you are about to start building your own professional website and if this is the case then we would like to first of all tell you congratulations. What you need to know about building your […]

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

  • Questions About You Must Know the Answers To
    Geospatial Questions About You Must Know the Answers To What You Need to Consider When Choosing the Best iPhone and Smartphone Repair Services Provider By having a phone there are many benefits that you will be able to get and this is why there are many people all over the world with phones and there […]
  • The Best Advice on I’ve found
    Geospatial The Best Advice on I’ve found Guide to Get the Best Healthy Movement Training Services It is always the main objective of everyone to see to it that they have the capability to enhance a more comfortable way of living. The essence is that it is when we are comfortable in all we do […]
  • – Getting Started & Next Steps
    Geospatial – Getting Started & Next Steps Choosing a Superb Aesthetics Skincare Center It is advisable for everyone to ensure that they are generally healthy. Skincare is crucial and required of everyone. Different people have different preferences and likes with how they want to look like this, there are people who will look for different […]
  • : 10 Mistakes that Most People Make
    Geospatial : 10 Mistakes that Most People Make Important Things to Consider When Buying Gemstone Emeralds have been identified to form a more significant part of beauty. Whenever you buy gemstone, you will need to ensure that you achieve the objectives for which you are buying them and ensure that all the problems are solved […]