Why is my method only returning the first row of my table? I can’t understand why and it’s driving me nuts. I’m sure it’s something very simple.
public function getTitlesForRegistrationForm() {
$result = $this->_db->query("SELECT UserTitleID, UserTitleName FROM UserTitles");
$i=0;
$array[0] = "No result";
foreach($result->fetch(PDO::FETCH_ASSOC) as $row){
$array[$i] = $row;
$i++;
}
return $array;
}
Thanks.
You need to
fetch()inside awhileloop. It will return only one row each time it is called.I’ve also taken the liberty of refactoring away your
$icounter. Instead theNo resultis appended onto the array in the first position (for whatever purpose you planned to use it), and subsequent rows are appended on with[].