检查成功的MySQL查询是否没有返回结果的最可靠方法是什么?

时间:2017-01-17 15:17:48

标签: php mysql isset mysql-num-rows

我想检查一个成功的查询是否没有返回任何结果。

在本地测试此脚本时,我确保数据库不包含查询正在寻找的任何值,但

mysqli_num_rows($res) != 0

返回true。

mysqli_num_rows($res) 

返回整数1和

echo json_encode($res)

返回

{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}

的script.php

$sql    = "SELECT AVG(numUsers) FROM attendance 
           WHERE date = '".$currDate."' 
           AND HOUR(time) = '".$currTime."' ";

$res    = mysqli_query($conn, $sql)or die(mysqli_error($conn));

if(mysqli_num_rows($res) != 0) {
    while($row = mysqli_fetch_array($res)) {
        $numUsers = $row['AVG(numUsers)'];
    }
    // Round this average value to the nearest integer
    $numUsers = round($numUsers);
}else{
    echo "No values have yet been inserted into 'attendance' during the current hour.";
}

// Insert $numUsers into another table 

这意味着一旦返回此结果数组, numUsers 变量将保持为空,并且

round($numUsers)

返回零,这是一个非法的值,稍后会插入到数据库中。

如何最可靠地检查成功查询是否返回了有效结果,并且相应地设置了 numUsers

0 个答案:

没有答案