[php] mysql query result into php array

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

This question is related to php mysql arrays

The answer is


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 />';
}

Use mysql_fetch_assoc instead of mysql_fetch_array

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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?