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);
}
fetch()returnsfalseon failure, like when there are no available rows. It's up to you to handle this and generate a blank array if you need it. Something likereturn $statement->fetch(PDO::FETCH_ASSOC) ?: [];. Also note thatfetch()only returns one (the next) row. If you have multiple rows, you need to keep callingfetch()until it returnsfalse(meaning you ran out of rows). Did you wantfetchAll()instead?