from mysql in associative array [message #183966] |
Fri, 29 November 2013 15:31 |
Knut Krueger
Messages: 1 Registered: November 2013
Karma: 0
|
Junior Member |
|
|
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' );
Regards Knut
|
|
|
|
|
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: 0
|
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.
|
|
|
Re: from mysql in associative array [message #183970 is a reply to message #183969] |
Fri, 29 November 2013 19:06 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 11/29/2013 11:47 AM, The Natural Philosopher wrote:
> 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.
>
You think wrong (as usual). Other than using mysql_xxx instead of
mysqli_xxx, Tonly's response is accurate.
Personally I would have used mysqli_fetch_assoc, but you still need to
build the new array as Tony indicated - just using indexed entries in an
array instead of members of an object.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: from mysql in associative array [message #183971 is a reply to message #183968] |
Fri, 29 November 2013 20:29 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
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>
|
|
|
|
Re: from mysql in associative array [message #183973 is a reply to message #183972] |
Fri, 29 November 2013 22:36 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Ben Bacarisse wrote:
> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>> Tony Mountifield wrote:
>>> I wrote:
> <snip>
>>>> $sh = mysql_query("SELECT id,value FROM mytable", $db);
>>
>> Unwise, see below.
>>
>>>> if ($sh) {
>>
>> Insufficient, RTFM.
>
> Can you say why?
Yes.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
|
|
|
Re: from mysql in associative array [message #183974 is a reply to message #183972] |
Fri, 29 November 2013 23:56 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Fri, 29 Nov 2013 22:26:30 +0000, Ben Bacarisse wrote:
> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>
>> Tony Mountifield wrote:
>>> I wrote:
>>>> if ($sh) {
>>
>> Insufficient, RTFM.
>
> Can you say why?
I'd guess tpel is postulating a resource with a non truthy value being
returned from mysql_query, although I also note that reading the manual
entry for mysql_query the section on return value does *_NOT_* contain
the standard warning about valid false return values.
'if ($sh) {}' appears to be fine according to the manual, which also
states that a resource is always truthy[1], unlike an int where 0 may be
a valid response that would evaluate as false, or a string where "" or
"0" could likewise be a valid return value which would evaluate to false.
The manual entry for array_search contains the following text in the
return value section:
"Warning
This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE. Please read the section on Booleans for
more information. Use the === operator for testing the return value of
this function."
However the manual entry for mysql_query does not contain this warning.
Of course, tpel might have some other rationale and it's always possible
he's not just talking out of his arse again.
[1] http://php.net/manual/en/language.types.boolean.php
" When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource). "
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
Re: from mysql in associative array [message #183975 is a reply to message #183974] |
Sat, 30 November 2013 00:30 |
Ben Bacarisse
Messages: 82 Registered: November 2013
Karma: 0
|
Member |
|
|
Denis McMahon <denismfmcmahon(at)gmail(dot)com> writes:
> On Fri, 29 Nov 2013 22:26:30 +0000, Ben Bacarisse wrote:
>
>> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>>
>>> Tony Mountifield wrote:
>>>> I wrote:
>
>>>> > if ($sh) {
>>>
>>> Insufficient, RTFM.
>>
>> Can you say why?
>
> I'd guess tpel is postulating a resource with a non truthy value being
> returned from mysql_query, although I also note that reading the manual
> entry for mysql_query the section on return value does *_NOT_* contain
> the standard warning about valid false return values.
>
> 'if ($sh) {}' appears to be fine according to the manual, which also
> states that a resource is always truthy[1],
Sure -- mysql_query is documented to return FALSE or a resource -- so it
looks fine to me, as least as far as recent PHP versions go.
I suppose I'd have to ask in a more literal way to coax more information
from him, but I am not sure it's worth it.
<snip>
--
Ben.
|
|
|
Re: from mysql in associative array [message #183976 is a reply to message #183972] |
Sat, 30 November 2013 01:29 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 11/29/2013 5:26 PM, Ben Bacarisse wrote:
> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>
>> Tony Mountifield wrote:
>>> I wrote:
> <snip>
>>>> $sh = mysql_query("SELECT id,value FROM mytable", $db);
>>
>> Unwise, see below.
>>
>>>> if ($sh) {
>>
>> Insufficient, RTFM.
>
> Can you say why?
>
> <snip>
>
Don't mind Pointed Head. He is unable to provide an intelligent
response to your question.
It actually is quite OK, because mysql_query will either return a
resource (which will always be non-false) or false.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
Re: from mysql in associative array [message #183982 is a reply to message #183980] |
Sat, 30 November 2013 14:28 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Tony Mountifield wrote:
> Thomas 'PointedEars' Lahn <php(at)PointedEars(dot)de> wrote:
>> That's what you get for not reading the newsgroup (sufficiently) before
>> you post.
>
> I'm puzzled how you can determine for how long I have been reading the
> newsgroup (quite a few years) from the fact that I rarely post to it.
“richard”.
HTH
PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
|
|
|