一个mysql中有多少行在fetch_assoc之前?

Uno*_*Ame 4 php mysqli

我通过select从mysql获取一些数据.

$result = mysqli_query($cxn, $query);
Run Code Online (Sandbox Code Playgroud)

然后我用它来完成它

if ($result!= false) {
    while ($row = mysqli_fetch_assoc($result)) {
        do_stuff();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想做的是while在$ only 之前添加一行,如果$ result中有多行.

我知道我可以首先创建一个数组,$row然后计算它们,但这会破坏它的便利性while.

问题:有没有办法找出mysqli_fetch_assoc在不运行两次的情况下会生成多少行?

mis*_*shu 7

你应该检查这个功能:http://php.net/mysqli_num_rows

您可以像这样使用它来检查结果的数量

if (mysqli_num_rows($result))
{
..
}
Run Code Online (Sandbox Code Playgroud)