且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

函数返回mysqli的查询结果只有第一个数组值

更新时间:2023-02-19 11:30:21

  $数据=阵列();
$查询=SELECT * FROM上传;
如果($结果= $ mysqli->查询($查询)){
  而($行= $ result-> FETCH_ASSOC()){
     $数据[] = $行;
  }
}
返回$的数据;

I have a small problem with my mysqli related function. Here is the code:

$query = "SELECT * FROM uploads";
 if ($result = $mysqli->query($query)) {
   while ($row = $result->fetch_assoc()) {
     return $row;
  }

The problem is that when I use this function $row appears to be an array, but with only the first value from the query result in it. But if I try to return var_dump($row) instead, function displays array as expected, with all the values from the query result. Can you please explain why is this happening and how to return an array with complete query result correctly. Thanks!

$data  = array();
$query = "SELECT * FROM uploads";
if ($result = $mysqli->query($query)) {
  while ($row = $result->fetch_assoc()) {
     $data[] = $row;
  }
}
return $data;