在PHP的HTML表中显示MySQL计数查询结果

时间:2019-05-28 07:00:29

标签: php mysql laravel-5

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

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

1 个答案:

答案 0 :(得分:0)

遵循以下示例:

<?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"; } ?>