note 110105 added to function.mysqli-multi-query
| From: | will at wacampbell dot com | Date: | Wed, 19 Sep 2012 19:34:09 +0000 |
| Subject: | note 110105 added to function.mysqli-multi-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190885@lists.php.net to get a copy of this message | ||
For all those out there looking for how to use the mysqli_multi_query() function to do multiple
SELECT queries in order to display various information, and who can't figure out for the life
of them how to get each query into it's own array for displaying the columns, here is a method
I used to achieve that goal: (I'm no professional, took me about 2 hours to figure this thing
out, any professionals interested in improving this code please fill in.)
say you have a table called "pages" that has "page_id", "name", and
"link" as its columns. and then another called "parts" that has
"part_id", "name", "link" as its columns.
You want to make two HTML tables displaying a list of the rows in these two tables, one for pages,
and one for parts, and you need to do specifically different things to each table. So you need to
store the information of both of these tables into their own array, then use a while statement to
display as needed.
<?php
$query = "SELECT * FROM pages;";
$query .= "SELECT * FROM parts";
$results = mysqli_multi_query($cxn, $query);
if ($results) {
$i = 0;
do {
$i ++;
if ($i == 1) {
$result = mysqli_store_result($dbc);
} else if ($i == 2) {
$result2 = mysqli_store_result($dbc);
}
} while (mysqli_next_result($dbc3));
echo "<h4>Pages List</h4><p>";
while ($row = mysqli_fetch_array($result)) {
echo $row['1']." ".$row['2']."<br />";
}
echo "</p>";
echo "<h4>Parts List</h4><p>";
while ($row2 = mysqli_fetch_array($result2)) {
echo $row2['1']." ".$row2['2']."<br />";
}
echo "</p>";
} else {
echo "<p>Main multi query failed</p>";
echo mysqli_error($dbc);
}
?>
That prints out (not a table, I know) two separate lists each for the separate tables.
----
Server IP: 134.129.167.88
Probable Submitter: 70.181.164.222
----
Manual Page -- http://www.php.net/manual/en/function.mysqli-multi-query.php
Edit -- https://master.php.net/note/edit/110105
Del: integrated -- https://master.php.net/note/delete/110105/integrated
Del: useless -- https://master.php.net/note/delete/110105/useless
Del: bad code -- https://master.php.net/note/delete/110105/bad+code
Del: spam -- https://master.php.net/note/delete/110105/spam
Del: non-english -- https://master.php.net/note/delete/110105/non-english
Del: in docs -- https://master.php.net/note/delete/110105/in+docs
Del: other reasons-- https://master.php.net/note/delete/110105
Reject -- https://master.php.net/note/reject/110105
Search -- https://master.php.net/manage/user-notes.php