Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12027

Re: from mysql in associative array

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!nerim.net!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2908877.REXbavQy2K@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 29 Nov 2013 21:29:17 +0100
User-Agent KNode/4.11.3
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: from mysql in associative array
Newsgroups comp.lang.php
References <l7abvs$k7s$1@dont-email.me> <l7acaj$ab9$1@softins.softins.co.uk> <l7achb$ab9$2@softins.softins.co.uk>
MIME-Version 1.0
Lines 64
NNTP-Posting-Date 29 Nov 2013 21:29:17 CET
NNTP-Posting-Host fdd10136.newsspool2.arcor-online.net
X-Trace DXC=Mlm;YWW4`cJgj[ZPFj7ehOA9EHlD;3YcB4Fo<]lROoRA8kF<OcfhCOKE[fcFYNkLVHDZm8W4\YJNLYUVHKH;GI^GJM2jo<YdY9NGj`B@QXYJbD
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.php:12027

Show key headers only | View raw


Tony Mountifield wrote:

> I wrote:
>> In article <l7abvs$k7s$1@dont-email.me>,
>> Knut Krueger  <knut.krueger@usa.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@94.75.214.39>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

from mysql in associative array Knut Krueger <knut.krueger@usa.com> - 2013-11-29 16:31 +0100
  Re: from mysql in associative array tony@mountifield.org (Tony Mountifield) - 2013-11-29 15:36 +0000
    Re: from mysql in associative array tony@mountifield.org (Tony Mountifield) - 2013-11-29 15:40 +0000
      Re: from mysql in associative array The Natural Philosopher <tnp@invalid.invalid> - 2013-11-29 16:47 +0000
        Re: from mysql in associative array Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-29 14:06 -0500
      Re: from mysql in associative array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-29 21:29 +0100
        Re: from mysql in associative array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-29 22:26 +0000
          Re: from mysql in associative array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-29 23:36 +0100
          Re: from mysql in associative array Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-29 23:56 +0000
            Re: from mysql in associative array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-30 00:30 +0000
          Re: from mysql in associative array Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-29 20:29 -0500
        Re: from mysql in associative array tony@mountifield.org (Tony Mountifield) - 2013-11-30 13:35 +0000
          Re: from mysql in associative array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-30 15:28 +0100

csiph-web