在php中的HTML表中显示MySQL计数查询结果
如何在php中的HTML表中显示MySQL COUNT()查询结果。在php中的HTML表中显示MySQL计数查询结果,php,mysql,laravel-5,Php,Mysql,Laravel 5,如何在php中的HTML表中显示MySQL COUNT()查询结果。 我使用了以下查询: select name, count(*) from contacts group by 1 having count(*) > 1; 请遵循以下示例: <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connectio
我使用了以下查询:
select name, count(*) from contacts group by 1 having count(*) > 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"; } ?>
名称
全部的
设置一个for循环,然后只打印数据。在laravel中检查此链接,可以找到更有效的方法使用视图文件中的循环,这将使他对如何使用循环或在视图文件中打印数据有更广泛的了解