Reputation: 5455
I am getting only one row, can someone please tell me how to get all the data from the table column of my database table ?
public function getCategories(){
$result = $this->db->query('SELECT * FROM newscat');
$rows = array();
while($row = $result->fetch_assoc()){
$rows[] = $row;
return $rows;
}
}
Upvotes: 1
Views: 5126
Reputation: 418
If you are using mysqli. Then you can use its apiFfetch_all to get all the rows at once.
For example : $array=$result->fetch_all(MYSQLI_ASSOC);
The above code will get all associated rows in the corresponding array.
Upvotes: 0
Reputation: 54032
do the minor change
public function getCategories(){
$result = $this->db->query('SELECT * FROM newscat');
$rows = array();
while($row = $result->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
Upvotes: 2
Reputation: 31524
Your problem is the return $rows;
. It should reside after the while
. The thing is that it will enter the while, put the first row in the array, and then immediately return it. What you want is to let the while
do its thing, and after the it finished, return the array.
Upvotes: 1
Reputation: 449525
You're returning from within the loop. That will break it in the first round.
return
outside the loop.
Upvotes: 7