简体   繁体   English

SQL select 多个 arrays 并填充一个表

[英]SQL select multiple arrays and populate one table

I am trying to pupulate 1 table with results from 2 select querys but from the same table.我正在尝试使用来自同一个表的 2 个 select 查询的结果填充 1 个表。 It is not working, one pupulates the other one shows only one result.. Is it posible to do this in one select as i think the problem is with the use of two fetch assoc.它不起作用,一个填充另一个只显示一个结果.. 是否可以在一个 select 中执行此操作,因为我认为问题在于使用两个 fetch assoc。

Question: How do i get the result of both of these querys presented in the table i created?问题:如何获得我创建的表中显示的这两个查询的结果?

<table align="left" style="width: auto; min-width: 700px; margin-bottom:20px;" class="deftable">
<thead><tr>
<th colspan="4">Top 10 users</th>
</tr>
</thead>
<thead><tr>
<th colspan="2">Top 10 Richest users</th>
<th colspan="2">Top 10 Bananaslappers</th>
</tr>
</thead>
<thead>
<tr><th>Name</th>
<th>Bananas</th>
<th>Name</th>
<th>Total slapped</th>
</tr></thead><tbody>
<?php

    $stmt88 = $mysqli->prepare("SELECT naam,geld FROM `gebruikers` ORDER BY geld DESC LIMIT 10 ");
    $stmt88->execute();
    $result = $stmt88->get_result(); //only works when nd_mysli is set on the server!
    $stmt88->close();
     while ($rowrich = $result->fetch_assoc()) {
        
    $stmt89 = $mysqli->prepare("SELECT naam,user_amountslapped FROM `gebruikers` ORDER BY user_amountslapped DESC LIMIT 10 ");
    $stmt89->execute();
    $result2 = $stmt89->get_result(); //only works when nd_mysli is set on the server!
    $stmt89->close();
    while ($rowrich2 = $result2->fetch_assoc()) {
     
 ?>
 
<tr><td >
<a class="online" href="profile.php?username=<?= htmlspecialchars($rowrich['naam']) ?>"><b><?= htmlspecialchars($rowrich['naam']) ?></b></a></td>
<td>&#127820;<?= htmlspecialchars(number_format($rowrich['geld'],0,",",".")) ?></td>

<td >
<a class="online" href="profile.php?username=<?= htmlspecialchars($rowrich2['naam']) ?>"><b><?= htmlspecialchars($rowrich2['naam']) ?></b></a></td>
<td>&#127820;<?= htmlspecialchars(number_format($rowrich2['user_amountslapped'],0,",",".")) ?></td>
</tr>
</tbody>
<? } }?></table><br>

You shouldn't have nested loops, that's creating a cross product between the two results, not processing them in parallel.你不应该有嵌套循环,这是在两个结果之间创建一个叉积,而不是并行处理它们。

You should have a single loop that fetches a row from each query.您应该有一个从每个查询中获取一行的循环。

<?php

    $stmt88 = $mysqli->prepare("SELECT naam,geld FROM `gebruikers` ORDER BY geld DESC LIMIT 10 ");
    $stmt88->execute();
    $result = $stmt88->get_result(); //only works when nd_mysli is set on the server!
    $stmt88->close();
    $stmt89 = $mysqli->prepare("SELECT naam,user_amountslapped FROM `gebruikers` ORDER BY user_amountslapped DESC LIMIT 10 ");
    $stmt89->execute();
    $result2 = $stmt89->get_result(); //only works when nd_mysli is set on the server!
    $stmt89->close();
    while (($rowrich = $result->fetch_assoc()) && ($rowrich2 = $result2->fetch_assoc())) {
 ?>
<tr><td >
<a class="online" href="profile.php?username=<?= htmlspecialchars($rowrich['naam']) ?>"><b><?= htmlspecialchars($rowrich['naam']) ?></b></a></td>
<td>&#127820;<?= htmlspecialchars(number_format($rowrich['geld'],0,",",".")) ?></td>

<td >
<a class="online" href="profile.php?username=<?= htmlspecialchars($rowrich2['naam']) ?>"><b><?= htmlspecialchars($rowrich2['naam']) ?></b></a></td>
<td>&#127820;<?= htmlspecialchars(number_format($rowrich2['user_amountslapped'],0,",",".")) ?></td>
</tr>
</tbody>
<? } ?></table><br>

Note that if either of the queries returns less than 10 rows, the table will stop at the shorter length.请注意,如果任一查询返回的行数少于 10 行,则表将在较短的长度处停止。 If you want the longer length, use ||如果您想要更长的长度,请使用|| in the while condition, and then check whether $rowrich or $rowrich2 is empty before outputting those columns.while条件下,然后在输出这些列之前检查$rowrich$rowrich2是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM