Monday 5 December 2016

PHP - Uncaught Error: Call to a member function *() on boolean

I have a sql request in PHP, the result of this query is put in class variable
But when tis request return no result, I would like have a null value in variable, for the moment I have a false boolean returned.



Could you help me ?
Thank you






class test
{
const table_name = 'test_table';

protected $Id;
protected $Nom;


public static function byId($id)
{
$values = array
(
':Id' => $id,
);

$sql = 'SELECT *FROM '.self::table_name.' WHERE Id=:Id';

$QueryPrep = self::getInstance()->prepare($sql);

if(empty($param))
{
$resultQuery = $QueryPrep->execute();
}
else
{
$resultQuery = $QueryPrep->execute($values);
}

if ($resultQuery !=false)

{
$res = $resultQuery->fetchObject(__CLASS__);
return $res;
}
else
{
return false;
}
}


public function get_Id()
{
return $this->Id;
}
}

$cmd_device_id = null;

$cmdDeviceObject = new test();
$cmd_device = $cmdDeviceObject->byId(999);

$cmd_device_id = $cmd_device->get_Id();
echo "cmd_device_id = ".var_dump($cmd_device_id);

?>


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...