简体   繁体   中英

error 500 with Call to a member function on bool

I am using Fat Free Framework but I got this error when trying to check non-existent data ID with dry() function my code below will throw Call to a member function dry() on bool error if I supply it with non-existent ID value. It should echo the Empty Cursor echo if the ID not found instead if the error. If I use an ID that is within the database, it will show the id exist.

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('/');

how do I check if the ID exists without the error?

If your load() method returns false when the data isn't found, then you should be checking for this before you try any further operations on this value.

I've also changed the test to be

if($data !== false) {

so that if your method returns some other value which looks like false (0, null etc), then this will ensure it only picks up 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('/');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM