简体   繁体   English

在HTML表中显示SQL查询(php)

[英]Displaying SQL Query in HTML table (php)

Trying to get counts from 2 different tables into a simple HTML table.试图从 2 个不同的表中获取计数到一个简单的 HTML 表中。

This is my code:这是我的代码:

$sql = "SELECT COUNT(*) FROM tasks";
$result = $conn->query($sql);
$rowcount=mysqli_num_rows($result);

if ($result->num_rows > 0) { ?>

<table style="width:100%">
  <tr>
    <th>Total</th> 
  </tr>
  <?php while($row = $result->fetch_assoc()) { ?>
  <tr>
    <td><?=$rowcount;?></td> 
  </tr>
  <?php } ?>
</table>

<?php } else { echo "0 results"; } ?>

When I run the code, it shows the amount of rows in the table, but it also creates the amount of rows with the number in it (ie 281 rows).当我运行代码时,它显示了表中的行数,但它也创建了其中包含数字的行数(即 281 行)。

Table桌子
281 281
281 281
281 281
281 etc. 281等

My idea was to copy and paste the above to show a second table set of results (if it was correct), but is there a better way of doing this?我的想法是复制并粘贴上面的内容以显示第二组结果表(如果正确的话),但是有更好的方法吗? I've been looking at how I would display SELECT (select COUNT(*) from tasks), (select count(*) from quotes) into the following format (HTML):我一直在研究如何将SELECT (select COUNT(*) from tasks), (select count(*) from quotes)显示为以下格式(HTML):

Table桌子 Count数数
Tasks任务 281 281
Quotes引号 42000 42000

First of all your query does produces only one row for a table, not 281. The second - I omit usage of prepared SQL statements with placeholders, which should always be used in a real project whenever applyable.首先,您的查询确实只为表生成一行,而不是 281。第二个 - 我省略了使用占位符准备好的 SQL 语句,只要适用,应始终在实际项目中使用。

$rows = [];
foreach(['Tasks', 'Quotes'] as $table ){
    $result = $conn->query("SELECT '$table' as 'table', count(*) as 'count' FROM $table");
    if( $result ) 
        $rows[] = $result->fetch_assoc();
}

if( empty( $rows ) )
    print "0 results";
else {?>
    <table style="width:100%">
        <tr><th>Table</th><th>Count</th></tr>
        <?=implode(
            "\n", 
            array_map(function($row){
                return "<tr><td>${row['table']}</td><td>${row['count']}</td></tr>";
            }, $rows)
        )?>
    </table>
<?php }

I've been looking at how I would display SELECT (select COUNT( ) from tasks), (select count( ) from quotes) into the following format (HTML)我一直在研究如何将 SELECT(从任务中选择 COUNT( ))、(从引号中选择 count( ))显示为以下格式 (HTML)

You can just run the queries query, and use the result of the first to create the first row of the table, then the result of the second to create the second row.您可以只运行 queries 查询,并使用第一个的结果创建表的第一行,然后使用第二个的结果创建第二行。 Since COUNT queries always return exactly 1 row when there's no GROUP BY, it's quite simple to do really:由于在没有 GROUP BY 时 COUNT 查询总是只返回 1 行,所以做起来非常简单:

$sql1 = "SELECT COUNT(*) FROM tasks";
$result1 = $conn->query($sql1);
if ($row = $result1->fetch_array()) $taskCount = $row[0];
else $taskCount = "error";

$sql2 = "SELECT COUNT(*) FROM quotes";
$result2 = $conn->query($sql2);
if ($row = $result2->fetch_array()) $quoteCount = $row[0];
else $quoteCount = "error";

?>
<table style="width:100%">
  <tr>
    <th>Table</th> 
    <th>Count</th> 
  </tr>
  <tr>
    <td>Tasks</td>
    <td><?php echo $taskCount; ?></td>
  </tr>
  <tr>
    <td>Quotes</td>
    <td><?php echo $quoteCount; ?></td>
  </tr>
</table>

Another way, if you want the HTML structure to be less repetitive / dependent on the tables queries, is to UNION the SELECT s into a single query:另一种方法,如果您希望 HTML 结构重复性较低/依赖于表查询,则将SELECT UNION到单个查询中:

$sql = "SELECT 'Tasks' AS 'table', COUNT(*) as 'count' FROM tasks";
$sql .= " UNION ";
$sql .= "SELECT 'Quotes' AS 'table', COUNT(*) as 'count' FROM quotes";
$result = $conn->query($sql);
?>

<table style="width:100%">
  <tr>
    <th>Table</th> 
    <th>Count</th> 
  </tr>
<?php
while ($row = $result->fetch_assoc()) { ?>
  <tr>
    <td><?php echo $row["table"]; ?></td>
    <td><?php echo $row["count"]; ?></td>
  </tr>
<?php 
}
?>
</table>

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

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