【问题标题】:Display MySQL count query result in a HTML table in php在 php 中的 HTML 表中显示 MySQL 计数查询结果
【发布时间】:2019-05-28 07:00:29
【问题描述】:

如何在 php.ini 的 HTML 表中显示 MySQL COUNT() 查询结果。
我使用了以下查询:

select name, count(*) from contacts group by 1 having count(*) > 1;

【问题讨论】:

标签: php mysql laravel-5


【解决方案1】:

按照这个例子:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "select name, count(*) as total from contacts group by 1 having count(*) > 1";
$result = $conn->query($sql);

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

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

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

【讨论】:

  • 在 laravel 中有更有效的在视图文件中使用循环的方法,是的,它会让他更广泛地了解如何使用循环或在视图文件中打印他的数据
猜你喜欢
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
相关资源
最近更新 更多