I have a function searchUser
, which return array user's data. The function works, however there is one problem. When the search
parameter is empty then all the records from the database are there. And then the function should not return anything, right?
public function searchUser(string $search): array
{
$sql = $this->pdo->prepare("SELECT user_id, first_name, last_name, avatar FROM user where first_name like :first_name or last_name like :last_name");
$Search = "%$search%";
$sql->bindParam(':first_name', $Search, PDO::PARAM_STR);
$sql->bindParam(':last_name', $Search, PDO::PARAM_STR);
$sql->execute();
$search = [];
if ($sql->rowCount()) {
while ($row = $sql->fetch()) {
$search[] = new Search(
$row['user_id'],
$row['first_name'],
$row['last_name'],
$row['avatar']);
}
}
return $search;
}