-1

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;
    }
netjav
  • 15
  • 5
  • And the question is ? (Because the question "And then the function should not return anything, right?" is opinion based, and not suited for SO .... ) – Luuk Jul 17 '22 at 08:15
  • If the search button was pressed and the field is then empty, then no record should be returned then, and now it returns all – netjav Jul 17 '22 at 08:16
  • When you need to return nothing when searching for nothing, you can always add an if-statement like: `if ($search='') {.....} else { ` do whatever you need to do when you have a search item. – Luuk Jul 17 '22 at 08:17
  • But if I add it then I mix the logic with persistence – netjav Jul 17 '22 at 08:19
  • As you use `"%$search%"`, with nothing in the field you end up with `"%%"` which will match all rows. – Nigel Ren Jul 17 '22 at 08:21
  • [Are questions of academic interest allowed on Stack Overflow?](https://meta.stackoverflow.com/questions/330170/are-questions-of-academic-interest-allowed-on-stack-overflow)... You might mix logic with persistence, but, who cares? – Luuk Jul 17 '22 at 08:21
  • BTW: Searching for a common-letter (like `e` or `a`) will (or might) also return a long list of names, so I do not see the problem that searching for an empty string will return all names. When the long list is the problem you can have a look at: [Use LIMIT to paginate results in MySQL query](https://stackoverflow.com/questions/11728704/use-limit-to-paginate-results-in-mysql-query) – Luuk Jul 17 '22 at 08:28
  • I add my modified code, you look if that correct ? – netjav Jul 17 '22 at 08:31
  • To know if your code is correct, or to get a code review, you should post it here: https://codereview.stackexchange.com/ – Luuk Jul 17 '22 at 08:33
  • The code works, but I want to know if I don't mix logic with perstistence – netjav Jul 17 '22 at 08:35

1 Answers1

-1

 public function searchUser(string $search): array|string
    {


        if ($search == '') {
            $noUser = 'No result';
            return $noUser;
        } else {

            $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;
    }
netjav
  • 15
  • 5