Php mysqli_multi_查询-将结果分隔为列
我有七个SQL查询正在使用mysqli_multi_query函数执行:Php mysqli_multi_查询-将结果分隔为列,php,mysql,mysqli,html-table,mysqli-multi-query,Php,Mysql,Mysqli,Html Table,Mysqli Multi Query,我有七个SQL查询正在使用mysqli_multi_query函数执行: if (mysqli_multi_query($conn, $airlinesql)) { do { if ($result = mysqli_store_result($conn)) { while ($row = mysqli_fetch_row($result)) { HELP!! } mysqli_free_result($result); }
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!!
七个查询中的每一个每行只返回一个元素,但是为每个查询返回的行的数量未知-甚至可能为空。我希望每个查询结果在HTML表中填充自己单独的列。换句话说,第一个查询可能返回5行,每行只有一个元素。我希望这5个元素都进入HTML表第一列中自己的行中。第二个查询结果可能返回1个元素。该元素应位于第二列的第1行。尽管每个查询返回的行数未知(甚至可能为空),但列数固定为7(因为这是查询数)。我猜将所有结果都扔到一个数组中是正确的想法,但我不知道如何使用while/for循环从数组中构建表,因为您通常是按行构建的,而我的想法是按列构建的,因为每个查询返回的行数未知。或者,也许有比使用mysqli\u multi\u查询更好的方法?谢谢你的帮助。同意CBroes的建议。我知道……这很难看,效率可能非常低,但它确实有效。谢谢你的帮助。我上一次做任何编码是在1992年使用FORTRAN,所以我需要所有我能得到的帮助
$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;
$alldata=array();
$colno=0;
$rowcount=0;
//执行多重查询
if(mysqli_多_查询($conn,$sql)){
做{
如果($result=mysqli\u store\u result($conn)){
$rowno=0;
while($row=mysqli\u fetch\u row($result)){
foreach(行作为$value){
$alldata[$rowno][$colno]=$value;
$rowno++;
}
//计算当前过程中的行数;保留最高值
如果($rowno>$rowcount){
$rowcount=$rowno;
}
}
$colno++;
mysqli_免费_结果($result);
}
}而(mysqli_next_result($conn));
}
//将数组转储到表中
$可输出。=“”;
对于($i=0;$i Yes),首先将该数据粘贴到多维数组中,并确定“最大”返回的最大结果数查询was-这是您要创建的表行数。因此,请使用一个简单的for
循环,然后在另一个for
循环中对七列进行查询。然后只需使用isset
检查数组是否在相应位置包含条目-如果是,则将该值作为表单元格内容输出,否则我想最重要的问题是为什么你需要使用mysqli\u多个查询?有什么办法可以避免吗?你能使用普通的mysqli查询吗?你能使用PDO吗?我有点希望看到mysqli\u更多的结果()
在你的代码片段中是这样的:我曾经认为多重查询函数很可爱——现在我从来没有看到调用它的任何好处。这对我个人来说是一种一时的时尚。我们可以看看你的7个sql字符串吗?你可能只需要一个带有UNION
s的单一查询吗?@mickusa我认为你不需要mysqli\u more\u results()
但是我同意这个函数太麻烦了,没有理由使用它。你为什么不费心编辑你的问题以包含7个查询?