调用 bool 上的成员函数时出现错误 500

error 500 with Call to a member function on bool

我正在使用 Fat Free Framework 但在尝试使用 dry() 函数检查不存在的数据 ID 时出现此错误 如果我为其提供不存在的 ID 值,我下面的代码将抛出 Call to a member function dry() on bool 错误。如果没有找到 ID,它应该回显 Empty Cursor echo,而不是如果错误。如果我使用数据库中的 ID,它会显示该 ID 存在。

if(is_numeric($param['id'])) 
        {
            $id = $param['id'];
            //check if data exist
            $data = $data->load($id);
            if(!$data->dry())
            {
                echo 'Data with id:'.$id.' exist';
            }
            else echo 'Cursor is empty. Data with id:'.$id.' doesnt exist!';

            die();
        }
        else $f3->reroute('/');

如何检查ID是否存在不报错?

如果您的 load() 方法 returns false 找不到数据,那么您应该在尝试对此值进行任何进一步操作之前检查它。

我也把测试改成了

if($data !== false) {

因此,如果您的方法 returns 其他一些看起来像 false(0、null 等)的值,那么这将确保它只获取 false...

if(is_numeric($param['id'])) 
{
     $id = $param['id'];
     //check if data exist
     $data = $data->load($id);
     if($data !== false) {
         echo 'Data with id:'.$id.' exist';
     }
     else  {
         echo 'Cursor is empty. Data with id:'.$id.' doesnt exist!';
     }
     die();
}
else {
    $f3->reroute('/');
}