php - mysqli_multi_query - Separate results into columns
Get the solution ↓↓↓I have seven SQL queries that are being executed using the mysqli_multi_query function:
if (mysqli_multi_query($conn, $airlinesql)) {
do {
if ($result = mysqli_store_result($conn)) {
while ($row = mysqli_fetch_row($result)) {
HELP!!
}
mysqli_free_result($result);
}
} while (mysqli_next_result($conn));
}
//Build and fill HTML table
HELP!!
Each of the seven queries returns only one element per row, but the number of rows returned for each query is unknown - could even be NULL. I want each query result to populate its own separate column in an HTML table. In other words, the first query may return 5 rows each having only one element. I want each these 5 elements to go into its own row in the first column of the HTML table. The second query result may return 1 element. This element should reside in row 1 of the second column. Etc. Although number of rows returned per query is unknown (and could even be null), the number of columns is fixed at 7 (since that is the number of queries). I'm guessing tossing all results into an array is the right idea, but I can't figure out how to build the table from the array using while/for loops since you typically build by row whereas my thinking is stuck with building by column due to the unknown number of rows returned per query. Or, maybe there's a better way than using mysqli_multi_query? Thanks for your help.
Answer
Solution:
Went with CBroes' suggestion. I know...it's ugly and probably horribly inefficient but IT WORKS. Thanks for your help. The last time I did any coding was using FORTRAN in 1992 so I need all the help I can get!
$alldata = array();
$colno = 0;
$rowcount = 0;
// Execute multi query
if (mysqli_multi_query($conn, $sql)) {
do {
if ($result = mysqli_store_result($conn)) {
$rowno = 0;
while ($row = mysqli_fetch_row($result)) {
foreach ($row as $value) {
$alldata[$rowno][$colno] = $value;
$rowno++;
}
//count rows in current pass; keep highest value
if ($rowno > $rowcount) {
$rowcount = $rowno;
}
}
$colno++;
mysqli_free_result($result);
}
} while (mysqli_next_result($conn));
}
//dump array into table
$outputtable .= "<table>";
for ($i = 0; $i <= $rowcount-1; $i++) {
$outputtable .= "<tr>";
for ($j = 0; $j <= $colcount; $j++) {
if (isset($alldata[$i][$j])) {
$outputtable .= "<td>" . $alldata[$i][$j] . "</td>";
}
$outputtable .= "</tr>";
}
}
$outputtable .= "</table>";
echo $outputtable;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.