I am using PHP 7.4 and my class have a function that should return an array. But if the row is empty it returns false instead of an empty array. That is why I get an error message:
Return value must be of type array, bool returned
private function getCartItem(int $sku): array
{
$statement = $this->pdo->prepare("SELECT * FROM pbo_cartItem WHERE cartId = ? AND sku = ?");
$statement->execute(array($this->cartId, $sku));
return $statement->fetch(PDO::FETCH_ASSOC);
}
The PDOStatement::fetch()
method returns false
if there are no more rows in the result set.
To overcome this error message, provide an empty array as a default value in case no rows were found.
return $statement->fetch(PDO::FETCH_ASSOC) ?: [];
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments