这里有一些代码,我希望用mysql将mysql查询结果存储到数组中,但是我的代码返回结果:2h
,而不是我想要的。(正确的结果应该是36,35,34,33, 32)
<?php
set_time_limit(59);
mysql_select_db("mycoon",$db);
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT id,link FROM mytable Order By id DESC LIMIT 0,5");
$new_array[] = $row;
while ($row = mysql_fetch_array($result)) {
$new_array[$row['id']] = $row;
$new_array[$row['link']] = $row;
}
mysql_close($db);// close mysql then do other job with set_time_limit(59)
foreach($new_array as $array){
echo $array['id'].'<br />';
echo $array['link'].'<br />';
}
?>
结果:
36
http://localhost/img/img36.jpg
36
http://localhost/img/img36.jpg
35
http://localhost/img/img35.jpg
35
http://localhost/img/img35.jpg
34
http://localhost/img/img34.jpg
34
http://localhost/img/img34.jpg
33
http://localhost/img/img33.jpg
33
http://localhost/img/img33.jpg
32
http://localhost/img/img32.jpg
32
http://localhost/img/img32.jpg
答案 0 :(得分:36)
我想你想这样做:
while( $row = mysql_fetch_assoc( $result)){
$new_array[] = $row; // Inside while loop
}
或者也可以将ID存储为密钥
$new_array[ $row['id']] = $row;
使用第二种方式,您可以直接通过其ID来处理行,例如:$new_array[ 5]
。
答案 1 :(得分:4)
使用mysql_fetch_assoc而不是mysql_fetch_array
答案 2 :(得分:3)
这个怎么样:
while ($row = mysql_fetch_array($result))
{
$new_array[$row['id']]['id'] = $row['id'];
$new_array[$row['id']]['link'] = $row['link'];
}
检索链接和ID:
foreach($new_array as $array)
{
echo $array['id'].'<br />';
echo $array['link'].'<br />';
}