如何获得PHP中一系列对象的记录计数



我正在尝试显示从我的mongo 3.4数据库中返回的记录总数。对于此特定查询,结果应为380,但显示为14489。我敢肯定,我缺少/忘记了PHP

很简单。

这是代码:(包括有关我要处理的变量类型的一些调试语句(

       $numrecords = count($records);                                
       echo"<BR><font color=red>".gettype($records)."</font>";
       echo"<BR><font color=red>".sizeof($records)."</font>";
       if (  $numrecords > 0 ) {
              echo "<tr><td colspan='5'><h3>Record Count: ".$numrecords ."</h3></td></tr>";
              echo "<tr><th>PH Number</th><th>Department</th></tr>";
              foreach ( $records as $rec ) {
                     if (!empty($rec->department)) {                                                                                          
                         echo "<tr>";
                         echo "<td>". $rec->phnum . "</td>";
                         echo "<td>". $rec->department . "</td>";
                         echo "</tr>";
                     }   
              } //end for 
       } else {
              echo "<tr><td colspan='5'>No matching data</td></tr>";                                  
       } 

它说对象类型是"数组"。我一直在玩count((vs.sizeof((

任何技巧都将不胜感激。

因此,它显示了表中正确的行数,但是计数显示的比显示的表中的数量更多?

尝试在查询之后使用msqli_num_rows

这是下面的一个示例,您必须根据您的需求将其更改为您的需求,因为您没有提供查询代码

$sql = "SELECT * FROM table_name WHERE datarow='$datarow'";
$result = mysqli_query($conn, $sql);
//this is where you count the number of rows returned from this query.
$count = mysqli_num_rows($result);

如果这不起作用,则您的查询不符合您想要的规格。

最新更新