Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Populating an array from a mysql select

by rinceWind (Monsignor)
on May 04, 2007 at 15:35 UTC ( [id://613596]=note: print w/replies, xml ) Need Help??


in reply to Populating an array from a mysql select

The point is that fetchrow_array only fetches a single row. You want to use fetchall_arrayref instead.

Warning: untested

my @userlist = map {$_->[0]} @{$select->fetchall_arrayref};

--
wetware hacker
(Qualified NLP Practitioner and Hypnotherapist)

Replies are listed 'Best First'.
Re^2: Populating an array from a mysql select
by Nik (Initiate) on May 04, 2007 at 17:37 UTC
    Thank you it does work :) but i didnt understood how as well i didnt understand why my
    @userlist = $select->fetchall_arrayref;
    returns a strign like HASH0xblabla...

      The reason is in what fetchall_arrayref returns. In general an SQL statement car return many columns, so you get array of rows, not an array of values. Each row in turn is an array ref or hash ref depending on how you call fetchrow_arrayref. In your case, you only have one column, and you can use fetchcol_arrayref to obtain that one column's data directly without havning to dereference anything for each row.

      Update: I misremembered the existence of a fetchcol_arrayref method. No such method exists; I was thinking of selectcol_arrayref which operates on a dbh, not a sth, and takes a statement as its first argument.

      A reply falls below the community's threshold of quality. You may see it by logging in.

      Allow me to explain.

      fetchall_arrayref returns an array ref, i.e. a scalar value which is a reference to an array (which will stringify as something like ARRAY(0x81503e8) if you try and print it out). You want to dereference this, and gain access to the array, hence the @{...}. This array has one element per row.

      Unfortunately, this is not quite exactly what you want, as it's an array of arrayrefs. Each of the referenced arrays only has one element, as your SQL is only returning one column, but would contain more than one value if your query was returning more columns (note: there is no such method as fetchcol_arrayref).

      The trick I have done is to fish out the element of each array, as the action for the map function. I could have written this as a for loop, but this kind of inline transformation lends itself to the builtins map and grep.

      For further reading, see perlreftut.

      --
      wetware hacker
      (Qualified NLP Practitioner and Hypnotherapist)

        An array reference is actually a pointer(as we would have said in C++) that points to the starting memoery location of the array?! And de-reference means to point to the value of memory location instead of the memory location itself?

        Also this: my @titlelist; open FILE, "<$ENV{'DOCUMENT_ROOT'}/data/vault/titlelist.txt" or die $!; @titlelist = <FILE>; close FILE; print br() x 3; print start_form( action=>'/cgi-bin/admin.pl' ); print table( {class=>'user_form'}, Tr( td( '&#928;&#941;&#962; &#956;&#959;&#965; &#964;&#953; &#95 +2;&#945; &#942;&#952;&#949;&#955;&#949;&#962;:' ), td( popup_menu( -name=>'title' -values=>\@titlelist ))), Tr( td( '&#922;&#940;&#964;&#953; &#940;&#955;&#955;&#959; &#960 +;&#959;&#965; &#952;&#945; &#942;&#952;&#949;&#955;&#949;&#962; &#957 +;&#945; &#963;&#967;&#959;&#955;&#953;&#940;&#963;&#949;&#953;&#962;? +' ), td( textarea( -name=>'remark', -rows=>4, -columns=>25 ))), Tr( td( '&#932;&#959; &#964;&#951;&#955;&#941;&#966;&#969;&#957; +&#959; &#949;&#960;&#953;&#954;&#959;&#953;&#957;&#969;&#957;&#943;&# +945;&#962; &#963;&#959;&#965; &#947;&#953;&#945; &#949;&#960;&#953;&# +946;&#949;&#946;&#945;&#943;&#969;&#963;&#951; &#949;&#943;&#957;&#945;&#953;:' ), td( textfield( -name=>'phone' )) +), Tr( td( a( {href=>'/cgi-bin/show.pl?name=showbook'}, font( {size=>3, color=>'yellow'}, '&#917;&#956;&#966;&#940;&#957;&#95 +3;&#963;&#951;!' ))), td( submit( '&#913;&#960;&#959;&#963;&#964;&#959;&#955;&#942 +;!' ))) ); print hidden(-name=>'date', -value=>$date); print hidden(-name=>'host', -value=>$host); print end_form();
        Although @titlelist is filled with all entries from titlelist.txt file(one entry per line), when it gets printed with popup_menu it displays nothing at all!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://613596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found