Mysql 结果成数组(PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2984005/
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
Mysql results into array (PHP)
提问by hellozimi
How can i convert mysql results (from mysql_fetch_array) into such a form?
如何将 mysql 结果(来自 mysql_fetch_array)转换成这样的形式?
$some = array(
     "comments" => array(
         array( "text" => "hi", "id" => "1" ),
         array( "text" => "hi", "id" => "2" ),
         array( "text" => "hi", "id" => "3" ),
         array( "text" => "hi", "id" => "4" )
    )
);
while the db looks like:
而数据库看起来像:
comments
注释
id  text
1   blabla bla
2   bla bla
i've tried to fetch the values with foreach/while and insert it into two arrays but no success...
我试图用 foreach/while 获取值并将其插入到两个数组中,但没有成功...
$some = array(
    "comments" => array()
);
$q = $mysql->sf('*', TBL_QST);
foreach($mysql->fetch($q) as $row) {
    $some[] = $row;
    // $some["comments"][] = $row;
}
回答by hellozimi
My print_r gives:
我的 print_r 给出:
Array
(
    [comments] => Array
        (
            [0] => Array
                (
                    [text] => lorem ipsum
                    [id] => 0
                )
            [1] => Array
                (
                    [text] => lorem ipsum
                    [id] => 1
                )
            [2] => Array
                (
                    [text] => lorem ipsum
                    [id] => 2
                )
        )
)
And the php I did for it is:
我为它做的 php 是:
$comments = array(
    'comments' => array()
    );
/* To simualate the loop */
for ($i = 0; $i < 3; $i++) { 
    array_push($comments['comments'], array(
        'text' => 'lorem ipsum',
        'id' => $i
        )
    );
}
I hope this will be to any help and that I didn't misunderstood your question.
我希望这会对您有所帮助,并且我没有误解您的问题。
回答by Christian Mann
Here's what I would do:
这是我会做的:
$dbc = mysql_connect(SERVER, USERNAME, PASSWORD);
mysql_select_db($dbc, DATABASE);
$query = "SELECT `id`, `text` FROM `comments`";
$result = mysql_query($dbc, $query);
$some = Array("comments" => Array());
while($row = mysql_fetch_array($result)) {
    array_push($some['comments'], Array($row['id'], $row['text']));
}
回答by draganHR
Use mysql_fetch_assoc()instead, or use MYSQL_ASSOCas second argument of mysql_fetch_array().
使用mysql_fetch_assoc()替代,或者用MYSQL_ASSOC作为第二个参数mysql_fetch_array()。
回答by zerkms
$some['comments'][] = ...
inside a loop
在一个循环内
回答by Bakhtiyor
What you want is an associative array, and for this reason a special function has been already developed in PHP which is called mysql_fetch_assoc().
你想要的是一个关联数组,因此在 PHP 中已经开发了一个名为mysql_fetch_assoc()的特殊函数。
Updated
I am sorry, I understood you wrong. In this case you need this block of code below. 
更新
对不起,我理解你错了。在这种情况下,您需要下面的这段代码。
$sql="select id,text from comments";
$res=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($res)){
     $arr[]=array('id'=>$row['id'],'text'=>$row['text']);
}
$some=array('comments'=>$arr);
回答by Misunderstood
Make an array with the same structure as the comments table:
制作一个与评论表结构相同的数组:
$results = mysql_query("SELECT `id`, `text` FROM `comments`");
while ($row = mysql_fetch_array($results, MYSQL_NUM)) {$comments[intval($row[0])] = $row[1];}
The comments array is now structured:
评论数组现在结构化:
comments[int `id`]=>`comment`
Then if you need commentsin the "some" array:
然后,如果您需要comments在“一些”数组中:
$some['comments'] = $comments;

