FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » from mysql in associative array
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: from mysql in associative array [message #183971 is a reply to message #183968] Fri, 29 November 2013 20:29 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
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();

Unnecessary, and – as-is – confusing.

>> $sh = mysql_query("SELECT id,value FROM mytable", $db);

Unwise, see below.

>> if ($sh) {

Insufficient, RTFM.

>> while ($ob = mysql_fetch_object($sh)) {
>> $array[$ob->value] = $ob->id;
>> }

It is unnecessary and inefficient to create a stdObject instance just to
throw it away. mysql(i)_fetch_assoc() suffices here; however
mysqli_fetch_all() and friends in combination with array_map() is even more
efficient.

>> $sh->finish();
>
> Of course, that should be mysql_free_result($sh);

Of course, that should be *at least* mysqli_free_result($sh);

> That's what I get for flipping between PHP and Perl DBI !

That's what you get for not reading the newsgroup (sufficiently) before you
post. See also the warning in <http://php.net/mysql_free_result>.


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom(at)94(dot)75(dot)214(dot)39>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Regarding split text and match from data base
Next Topic: Review Copies Available for Learning FuelPHP for Effective PHP Development
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 04:11:35 GMT 2024

Total time taken to generate the page: 0.06773 seconds