mysql query result into php array

前端 未结 3 1664
灰色年华
灰色年华 2020-12-31 10:31

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

相关标签:
3条回答
  • 2020-12-31 10:37

    Use mysql_fetch_assoc instead of mysql_fetch_array

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

    0 讨论(0)
  • 2020-12-31 10:57

    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].

    0 讨论(0)
  • 2020-12-31 11:00

    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 />';
    }
    
    0 讨论(0)
提交回复
热议问题