Php 计算SELECT with WHERE子句中的行数
我有一个表格,有两列“社区”和“状态”,格式如下:Php 计算SELECT with WHERE子句中的行数,php,mysql,mysqli,Php,Mysql,Mysqli,我有一个表格,有两列“社区”和“状态”,格式如下: community status Zoo 1 Zoo 1 Zoo 0 Zoo 1 现在我想计算一下,动物园有多少行的状态为1 对于上面的示例,我希望输出类似“No of rows is 3” 现在,我可以查询Zoo列状态为1的行,并返回结果以获得类似111的输出 代码片段如下所示: if (mysqli_connect_errno()) {
community status
Zoo 1
Zoo 1
Zoo 0
Zoo 1
现在我想计算一下,动物园有多少行的状态为1强>
对于上面的示例,我希望输出类似“No of rows is 3”
现在,我可以查询Zoo列状态为1的行,并返回结果以获得类似111的输出
代码片段如下所示:
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM location WHERE (community = 'Zoo') AND (status = '1');";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
echo $row['status'];
}
$result->close();
对于希望在将来为特定查询计算行数的用户: 注意:在带有count(*)和echo$row['total']的查询中使用作为总计**
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT count(*) AS total FROM location WHERE (community = 'Zoo') AND (status = '1');";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
echo $row['total'];
}
$result->close();
输出:3,因为Zoo的列状态为1的行总数为3。只需使用mysqli_num_rows()强>函数
$q="select * from location where community='Zoo' AND status='1'";
$res=mysqli_query($con,$q);
echo mysqli_num_rows($res);
您是否尝试过在sql中使用恰当命名的
COUNT
函数?尝试从(社区='Zoo')和(状态='1')的位置选择count(*)代码>@Utsav嗨,谢谢,伙计,成功了。