首页 文章

计算php中函数发送的输出数据中的行数

提问于
浏览
-1

以下代码位于我的function.php文件中

function getAllData()
{
  $sql="SELECT * FROM `student`";
  $query=mysql_query($sql)or die(mysql_error());
    while($row=mysql_fetch_array($query))
    {
      $data[]=$row;
    }
  return $data;
}

在我的index.php中,我做了一些任务

$data=getAllData();
foreach($data as $row)
{
.
.Processing xx of xx records
.
}

我想找出查询输出的总行数,并在echo语句中填充xx . 请帮我解决php语法问题 . 我尝试了很多在互联网上找到的语法,但要么是回声空白(根本没有),要么是回声数组,而不是确切数 . 谢谢 .

编辑:是的,这个问题可能已经在Count number of MySQL rows找到了答案 . 抱歉 .

1 回答

  • 2

    然后就是这么简单:

    $data=getAllData();
    $counted = count($data);
    $i = 1;
    foreach($data as $row)
    {
    .
    .Processing $i of $counted records
    .
    $i++;
    }
    

    和!我强烈建议您使用PDO . 查找文档HERE

    希望这可以帮助! :d

相关问题