-2

I'm new to PHP and was wondering why my like query is only returning one value.

Here is my Model

//For search
public function getSearchResults($username){
    $stmt = $this->_connection->prepare("SELECT * FROM users WHERE username LIKE :username");
    $stmt->execute(['username' => '%'.$username.'%']);
    $stmt->setFetchMode(PDO::FETCH_CLASS, "User");
    return $stmt->fetch();
}

Then in my controller

function search($name){

    //Get search results
    $searchResults = $this->model('User');
    $searchResults = $searchResults->getSearchResults($name);
    $data = (array) $searchResults;

    $this->view('User/search', $data);
}

However, it is only returning 1 result rather than 3. (I have 4 usernames with "hi" inside it, but it's only returning one)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Cracks
  • 9
  • 1
  • 5

1 Answers1

0

When you call fetch() - this only returns the one row, to return all of the rows from the result set use fetchAll()

return $stmt->fetchAll();
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55