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