Using PHP to Process More than One SQL Statement at the Same Time

in #utopian-io7 years ago (edited)

It is very easy to work with MySQL queries and PHP and it's nearly as easy to process more than one SQL statement at the same time. Just use the mysqli multi_query method.

It’s very easy to send SQL queries to a MYSQL database and process the results. 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
('Creating a Distributed Web App ',
'https://utopian.io/utopian-io/@alv/creating-a-distributed-web-app-with-javascript-php-and-mysql');";
$sql .= "insert into articles
(title, url)
values
('How to Get User Information with PHP',
'https://utopian.io/utopian-io/@alv/how-to-get-user-information-with-php-use-php-to-ask-a-web-page-user-questions-and-process-the-answers');";
$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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

By Using PHP to Process is More valuable than One SQL Statement at the Same Time, this is also best and informative

Nice article. Learned something new today.

Thanks for the informative article! I'm still learning PHP & MySQL and it answered all of my questions. :)

Really nice...

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @alv I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.030
BTC 62907.89
ETH 3379.73
USDT 1.00
SBD 2.50