Re: from mysql in associative array [message #183969 is a reply to message #183968] |
Fri, 29 November 2013 16:47 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 29/11/13 15:40, Tony Mountifield wrote:
> I wrote:
>> In article <l7abvs$k7s$1(at)dont-email(dot)me>,
>> Knut Krueger <knut(dot)krueger(at)usa(dot)com> wrote:
>>> Hi to all, whats the best was to get to get one of two colums of an
>>> database query in an array as the names of the associative array
>>>
>>> means
>>> mysql table
>>> id value
>>> 1 green
>>> 2 red
>>> 3 blue
>>> ....
>>>
>>>
>>> should be
>>>
>>> $array = array ( 'green ' => '1',
>>> 'red ' => '2',
>>> 'blue ' => '3' );
>>
>> I would do something like:
>>
>> $array = array();
>>
>> $sh = mysql_query("SELECT id,value FROM mytable", $db);
>> if ($sh) {
>> while ($ob = mysql_fetch_object($sh)) {
>> $array[$ob->value] = $ob->id;
>> }
>> $sh->finish();
>
> Of course, that should be mysql_free_result($sh);
>
> That's what I get for flipping between PHP and Perl DBI !
>
Tsk It uses the deprecated Mysql functions..
I think if the query is 'order by id' and id is unique and you do a
myslqli_fetch_assoc then you can get the array already parcelled up with
red tape on.
>> }
>>
>> Cheers
>> Tony
>> --
>> Tony Mountifield
>> Work: tony(at)softins(dot)co(dot)uk - http://www.softins.co.uk
>> Play: tony(at)mountifield(dot)org - http://tony.mountifield.org
>
>
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
|
|
|