简体   繁体   English

PHP查询以数组格式返回数据库

[英]PHP query to database return in array format

Let's say I query my database and I get the following result 假设我查询数据库并得到以下结果

+---+---+-------+
| N | M | score |
+---+---+-------+
| 0 | 2 |    14 |
| 1 | 0 |    22 |
+---+---+-------+

After doing $res = $db->query($query); 在执行$res = $db->query($query); Is there anyway to store this information in an array. 无论如何,是否有将该信息存储在数组中的信息。 For example, I've tried doing this 例如,我尝试这样做

      $array = array();    
      while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            array_push($array, array($row[0], $row[1], $row[2]));
      }

Because I have this idea that everytime it fetches a row into a numeric array, 因为我有这样的想法,每次它都会将一行读取到一个数值数组中,

$row[0] = 0 , $row[1] = 2, $row[2] = 14 for the first row and $row[0] = 0 , $row[1] = 2, $row[2] = 14第一行,

$row[0] = 1, $row[1] = 0, $row[2] = 22 for the second row $row[0] = 1, $row[1] = 0, $row[2] = 22第二行

Please do correct me if I'm wrong because I'm pretty sure I'm wrong :O 如果我错了,请纠正我,因为我敢肯定我错了:O

EDIT: Reason why I'm asking what $row contains is because I would like to store each row into an array to form a multi dimensional array. 编辑:我问$ row包含的原因是因为我想将每一行存储到一个数组中以形成一个多维数组。 For example, {"E":[[0,1,86],[1,0,96]]} after json_encode . 例如, json_encode之后的{"E":[[0,1,86],[1,0,96]]} So in order to do that, I need to push each row as an array into another array ie push the first row as array [0,1,86] into $array followed by pushing the second row as array [1,0,96] . 因此,为了做到这一点,我需要将每行作为数组推入另一个数组, [0,1,86]第一行作为数组[0,1,86]推入$array然后将第二行作为数组[1,0,96] [0,1,86][1,0,96] I doubt I can achieve this by doing an associative array. 我怀疑我可以通过执行关联数组来实现这一点。 once again correct me if I'm wrong. 如果我错了,请再次纠正我。

Your code is substantially fine ( mysql_fetch_array is a typo, I think). 您的代码基本正常(我认为mysql_fetch_array是一个错字)。

The only note: You have to init $array before while loop, otherwise your array_push will fail. 唯一需要注意的是:您必须在while循环之前初始化$array ,否则array_push将失败。

So, your code is working, but solution provided in comment is better, because is shorter and more efficient: with mysql_fetch_array you already obtain your desired array, so you have simply to add it to your array, without specifying each key. 因此,您的代码可以正常工作,但注释中提供的解决方案更好,因为它更短且更有效:使用mysql_fetch_array您已经获得了所需的数组,因此您只需将其添加到数组中,而无需指定每个键。 Also, as per php docs , the $array[] = syntax is more efficient that array_push() if adding only one element to array. 另外,根据php docs ,如果仅向数组添加一个元素,则$array[] =语法比array_push()更有效。

$array = array();
while( $row = mysqli_fetch_array( $res, MYSQL_NUM ) )
{
    $array[] = $row;
}

If you prefer, you can obtain same result using mysqli_fetch_all() : 如果愿意,可以使用mysqli_fetch_all()获得相同的结果:

$array = mysqli_fetch_all( $res, MYSQLI_NUM );

[Edit:] Obviously, the last two solutions will work only if you want retrieve all fields returned by the query. [编辑:]显然,仅当您要检索查询返回的所有字段时,后两种解决方案才有效。

[Edit 2:] To obtain JSON in your desired format, simply add this line at the end of your code: [编辑2:]要以所需的格式获取JSON,只需在代码末尾添加以下行:

$json = json_encode( array( 'E' => $array ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM