16

Some code here, I want store mysql query result into an array with php, but my code return result: 2h, not what I wish.(the correct result should be 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 />';
}
?>

Result:

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
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
fish man
  • 2,666
  • 21
  • 54
  • 94
  • 1
    Your SQL seems to be invalid: `WHERE Order By` - missing `WHERE` condition. – hsz Jan 23 '12 at 10:23
  • @hsz, right. indeed, my original code is very long, for an easy ask, I have shorted my code, but forget not remove `where`, thanks. – fish man Jan 23 '12 at 10:26
  • Dup of [Can you return an associated array with a number index?](http://stackoverflow.com/questions/339371/), [For each result in MySQL query, push to array (complicated)](http://stackoverflow.com/questions/3047896/) and ***many*** others (how many can we find?). – outis Jan 23 '12 at 10:36

3 Answers3

53

I think you wanted to do this:

while( $row = mysql_fetch_assoc( $result)){
    $new_array[] = $row; // Inside while loop
}

Or maybe store id as key too

 $new_array[ $row['id']] = $row;

Using the second ways you would be able to address rows directly by their id, such as: $new_array[ 5].

Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • @fishman I've update it. Did you use it like this, or did you put `$new_array[]` twice there? – Vyktor Jan 23 '12 at 10:34
4

Use mysql_fetch_assoc instead of mysql_fetch_array

http://php.net/manual/en/function.mysql-fetch-assoc.php

makriria
  • 383
  • 1
  • 9
  • It's kinda funny to see that the function that makes an array is called assoc instead array :-) – m3nda Jul 24 '17 at 16:02
  • 1
    mysql_fetch_assoc is deprecated since PHP 7.0: http://php.net/manual/en/function.mysql-fetch-assoc.php Use mysqli_fetch_assoc instead – Jonny Jan 19 '18 at 12:37
4

What about this:

while ($row = mysql_fetch_array($result)) 
{
    $new_array[$row['id']]['id'] = $row['id'];
    $new_array[$row['id']]['link'] = $row['link'];
}

To retrieve link and id:

foreach($new_array as $array)
{       
   echo $array['id'].'<br />';
   echo $array['link'].'<br />';
}
Zul
  • 3,627
  • 3
  • 21
  • 35
  • You're not initializing `$new_array[ $row['id']]` as array. Wouldn't `$new_array[ $row['id']] = array( 'id' => $row['id'], 'link' => $row['link']);` (assuming that you WANT and need change values/keys) work faster? – Vyktor Jan 23 '12 at 10:48
  • $row['id'] is dynamic so in a loop, $new_array[$row['id']] looks like: $new_array[1] = '...', $new_array[2] = '...',$new_array[3] = '...' ,etc, try print_r($new_array); – Zul Jan 23 '12 at 10:59