php 如何将 SQL 查询的所有结果存储在多维数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5353905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:07:29  来源:igfitidea点击:

How do I store all results from an SQL query in a multidimensional array?

phpsql

提问by umar

hello every one i want to convert my array into other array type , plz help me out . I m using this

大家好,我想将我的数组转换为其他数组类型,请帮帮我。我正在使用这个

$row = mysql_fetch_array($result, MYSQL_ASSOC); 

and output is

和输出是

Array ( [user_id] => 250 [name] => a [age] => sfsf [pic_path] => )

but i want the the output in this format

但我想要这种格式的输出

Array ( [0] => Array ( [user_id] => 250 [name] => a [age] => sfsf [pic_path] => ) [1] => Array ( [user_id] => 251 [name] => b [age] => sfsfs [pic_path] => ) )

so what function i should us to get the array in this format

那么我应该用什么函数来获取这种格式的数组

回答by Brad Christie

$data = array(); // create a variable to hold the information
while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false){
  $data[] = $row; // add the row in to the results (data) array
}

print_r($data); // print result


Update Feb '17Now that it's 2017, it's advised you use PDOs over mysql_*. A lot safer and can return this natively with fetchAll(as shown in `TrexXx's answer).

2017 年2 月更新现在是 2017 年,建议您在 mysql_* 上使用 PDO。更安全,并且可以使用本机返回fetchAll(如`TrexXx's answer所示)。

回答by mravey

The only way to avoid doing a loop would to use PDO (http://fr2.php.net/manual/fr/book.pdo.php) which is a better way to access database in PHP and you could do this :

避免循环的唯一方法是使用 PDO (http://fr2.php.net/manual/fr/book.pdo.php),这是在 PHP 中访问数据库的更好方法,您可以这样做:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');

$q = $pdo->query('select * from yourtable');
$r = $q->fetchAll();

var_dump($r);

回答by Matteo Riva

If you need to store all rows returned by the query in an array, try this:

如果您需要将查询返回的所有行存储在一个数组中,请尝试以下操作:

$result = array(); 

while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) ){
    $result[] = $row
}

回答by Mark Baker

$row = array();
$row[] = mysql_fetch_array($result, MYSQL_ASSOC);

Normally you'd assign the results like this in a loop

通常你会在循环中分配这样的结果

$rows = array();
while ($result = mysql_fetch_array($result, MYSQL_ASSOC)) {       
    $rows[] = $result;
}

回答by JYelton

You can't get a multi-dimensional array from the resultset of your SQL query in just one command. You need to repeat the function mysql_fetch_arrayonce per SQL result row.

您无法仅通过一个命令从 SQL 查询的结果集中获取多维数组。您需要为mysql_fetch_array每个 SQL 结果行重复该函数一次。

Also you can use mysql_fetch_assocto get the results as an associative array.

您也可以使用mysql_fetch_assoc关联数组来获取结果。

To achieve your goal:

为了实现您的目标:

$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++)
{
    $myArray[] = mysql_fetch_assoc($result);
}