函数返回mysqli的查询结果只有第一个数组值 [英] function returns only first array value with mysqli query result

查看:154
本文介绍了函数返回mysqli的查询结果只有第一个数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我的mysqli相关的功能。这里是code:

  $查询=SELECT * FROM上传;
 如果($结果= $ mysqli->查询($查询)){
   而($行= $ result-> FETCH_ASSOC()){
     返回$行;
  }

问题是,当我使用此功能 $行似乎是一个数组,但只有从查询结果中它的第一个值。
但是,如果我试图回报的var_dump($行)代替,功能显示数组作为预期,从查询结果中所有的值。
能否请您解释为什么会出现这种情况,如何正确地返回完整查询结果的数组。
谢谢!


解决方案

  $数据=阵列();
$查询=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;

这篇关于函数返回mysqli的查询结果只有第一个数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆