how do we use mysqli properly to fetch all the rec

2019-08-13 03:26发布

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;
           }
     }

标签: php mysql mysqli
4条回答
Bombasti
2楼-- · 2019-08-13 03:47

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.

查看更多
仙女界的扛把子
3楼-- · 2019-08-13 03:55

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.

查看更多
小情绪 Triste *
4楼-- · 2019-08-13 03:59

You're returning from within the loop. That will break it in the first round.

return outside the loop.

查看更多
别忘想泡老子
5楼-- · 2019-08-13 04:03

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;
 }
查看更多
登录 后发表回答