我有一个包含两列的表格' community'和'状态'采用以下格式:
community status
Zoo 1
Zoo 1
Zoo 0
Zoo 1
现在我想计算一下状态为1的行数为Zoo; 对于上面的例子,我希望输出类似于"行数不是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();
有人可以在这里向我展示或正确实施mysql_num_rows吗?
答案 0 :(得分:2)
只需使用 mysqli_num_rows(); 功能
$q="select * from location where community='Zoo' AND status='1'";
$res=mysqli_query($con,$q);
echo mysqli_num_rows($res);
答案 1 :(得分:0)
对于那些希望将来计算特定查询行数的人: 注意:在查询中使用 AS total ,使用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。