Populating an array from a mysql select

Discussion in 'Perl Misc' started by Nikos, May 4, 2007.

  1. Nikos

    Nikos Guest

    $select = $db->prepare( "SELECT username FROM users" );
    $select->execute;
    my @userlist = $select->fetchrow_array;

    print start_form( action=>'/cgi-bin/index.pl' );
    print h1( {class=>'lime'}, "ÅðÝëåîå ôï êåßìåíï ðïõ óå åíäéáöÝñåé
    => ",
    popup_menu( -name=>'select', -values=>
    \@userlist ),
    submit('ÅìöÜíéóç'));
    print end_form;


    In the above code insteaf od the array filling by all usernmes
    selected from the dataabse it only populates the 1st one.

    I want all users to be listed on the pop up menu.
    Nikos, May 4, 2007
    #1
    1. Advertising

  2. Nikos

    Mumia W. Guest

    On 05/04/2007 10:24 AM, Nikos wrote:
    > $select = $db->prepare( "SELECT username FROM users" );
    > $select->execute;
    > my @userlist = $select->fetchrow_array;

    ^^^^^^^^^^^^^^
    >
    > print start_form( action=>'/cgi-bin/index.pl' );
    > print h1( {class=>'lime'}, "Επέλεξε το κείμενο που σε ενδιαφέÏει
    > => ",
    > popup_menu( -name=>'select', -values=>
    > \@userlist ),
    > submit('Εμφάνιση'));
    > print end_form;
    >
    >
    > In the above code insteaf od the array filling by all usernmes
    > selected from the dataabse it only populates the 1st one.
    >
    > I want all users to be listed on the pop up menu.
    >


    Fetchrow_array only gets a single row at a time. Perhaps you want
    something like this (untested):

    my @userlist = map $_->[0], @{$select->fetchall_arrayref};
    Mumia W., May 4, 2007
    #2
    1. Advertising

  3. Nikos

    Nikos Guest

    On May 4, 6:58 pm, "Mumia W." <paduille.4061.mumia.w
    > wrote:
    > On 05/04/2007 10:24 AM, Nikos wrote:> $select = $db->prepare( "SELECT username FROM users" );
    > > $select->execute;
    > > my @userlist = $select->fetchrow_array;

    >
    > ^^^^^^^^^^^^^^
    >
    >
    >
    > > print start_form( action=>'/cgi-bin/index.pl' );
    > > print h1( {class=>'lime'}, "ÅðÝëåîå ôï êåßìåíï ðïõ óå åíäéáöÝñåé
    > > => ",
    > > popup_menu( -name=>'select', -values=>
    > > \@userlist ),
    > > submit('ÅìöÜíéóç'));
    > > print end_form;

    >
    > > In the above code insteaf od the array filling by all usernmes
    > > selected from the dataabse it only populates the 1st one.

    >
    > > I want all users to be listed on the pop up menu.

    >
    > Fetchrow_array only gets a single row at a time. Perhaps you want
    > something like this (untested):
    >
    > my @userlist = map $_->[0], @{$select->fetchall_arrayref};


    Thank you this worked actually:

    $select = $db->prepare( "SELECT username FROM users" );
    $select->execute;
    my @userlist = map {$_->[0]} @{$select->fetchall_arrayref};

    but this didnt which seems simpler as another OP suggested
    unfortunately:

    my $userlist = $db->selectcol_arrayref("SELECT username FROM users");

    returns ARRAY(0x11101)blabla instead of the real valeus why?
    Nikos, May 4, 2007
    #3
  4. In article <>,
    Nikos <> wrote:
    >my @userlist = map {$_->[0]} @{$select->fetchall_arrayref};
    >
    >but this didnt which seems simpler as another OP suggested
    >unfortunately:
    >
    >my $userlist = $db->selectcol_arrayref("SELECT username FROM users");
    >
    >returns ARRAY(0x11101)blabla instead of the real valeus why?


    Because it returns an array reference (as indicated by its name).

    To get to the real values you can use @$userlist (where you now
    use @userlist) or $userlist->[0] (where you now use $userlist[0]).

    Or, if you want to keep the rest of your code unchanged,

    my $userlist_ref = $db->selectcol_arrayref("SELECT username FROM users");
    my @userlist = @$userlist_ref;

    Gary
    Gary E. Ansok, May 4, 2007
    #4
  5. Nikos

    Nikos Guest

    On May 4, 10:15 pm, (Gary E. Ansok) wrote:
    > In article <>,
    >
    > Nikos <> wrote:
    > >my @userlist = map {$_->[0]} @{$select->fetchall_arrayref};

    >
    > >but this didnt which seems simpler as another OP suggested
    > >unfortunately:

    >
    > >my $userlist = $db->selectcol_arrayref("SELECT username FROM users");

    >
    > >returns ARRAY(0x11101)blabla instead of the real valeus why?

    >
    > Because it returns an array reference (as indicated by its name).
    >
    > To get to the real values you can use @$userlist (where you now
    > use @userlist) or $userlist->[0] (where you now use $userlist[0]).
    >
    > Or, if you want to keep the rest of your code unchanged,
    >
    > my $userlist_ref = $db->selectcol_arrayref("SELECT username FROM users");
    > my @userlist = @$userlist_ref;
    >
    > Gary


    Thanks Garry!

    An array reference is actually a pointer(as we would have said in C++)
    that points to the starting memoery location of the array?!
    Nikos, May 4, 2007
    #5
  6. On Fri, 4 May 2007 19:15:32 +0000 (UTC),
    (Gary E. Ansok) wrote:

    >my $userlist_ref = $db->selectcol_arrayref("SELECT username FROM users");
    >my @userlist = @$userlist_ref;


    Without creating an intermediate variable:

    my @userlist = @{ $db->selectcol_arrayref("SELECT username FROM
    users") };


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 4, 2007
    #6
  7. On 4 May 2007 12:21:36 -0700, Nikos <> wrote:

    >An array reference is actually a pointer(as we would have said in C++)
    >that points to the starting memoery location of the array?!


    Not really. But that is an analogy that may help you to understand.
    For more info refer to

    perldoc perlref


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 4, 2007
    #7
  8. Nikos

    Nikos Guest

    On May 4, 10:43 pm, Michele Dondi <> wrote:
    > On 4 May 2007 12:21:36 -0700, Nikos <> wrote:
    >
    > >An array reference is actually a pointer(as we would have said in C++)
    > >that points to the starting memoery location of the array?!

    >
    > Not really. But that is an analogy that may help you to understand.
    > For more info refer to


    Thank you, so now i know that my visualization analogy is not
    erroneous.

    Can you please help me with the other questions regarding @titlelist
    that is not ebing displayed?
    Nikos, May 4, 2007
    #8
  9. On 4 May 2007 12:59:44 -0700, Nikos <> wrote:

    >> Not really. But that is an analogy that may help you to understand.
    >> For more info refer to

    >
    >Thank you, so now i know that my visualization analogy is not
    >erroneous.
    >
    >Can you please help me with the other questions regarding @titlelist
    >that is not ebing displayed?


    Nope, I saw that post, but I have a hard time only looking at it,
    without even trying to read it accurately. And it's by far *not* my
    area of expertise, anyway. Now, do I have an area of expertise at all?


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 4, 2007
    #9
  10. Nikos

    Nikos Guest

    ill ry to make it simpler then:

    my @titlelist;
    open FILE, "<$ENV{'DOCUMENT_ROOT'}/data/vault/titlelist.txt" or die
    $!;
    @titlelist = <FILE>;
    close FILE;

    i try to print popup_menu( -name=>'title' -values=>\@titlelist ) but
    it return nothing.

    if i try to print @titlelist is filled though...
    Nikos, May 4, 2007
    #10
  11. Michele Dondi <> wrote:
    > On Fri, 4 May 2007 19:15:32 +0000 (UTC),
    > (Gary E. Ansok) wrote:
    >
    >>my $userlist_ref = $db->selectcol_arrayref("SELECT username FROM users");
    >>my @userlist = @$userlist_ref;

    >
    > Without creating an intermediate variable:
    >
    > my @userlist = @{ $db->selectcol_arrayref("SELECT username FROM
    > users") };



    For those of you playing along at home, Michele is
    demonstrating "Use Rule 1" from:

    perldoc perlreftut


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 4, 2007
    #11
  12. Nikos

    Nikos Guest

    On May 5, 1:50 am, Tad McClellan <> wrote:

    > For those of you playing along at home, Michele is
    > demonstrating "Use Rule 1" from:



    I read it that why i use a backslash before the array but stilll the
    array remains the same.
    Nikos, May 5, 2007
    #12
  13. On 4 May 2007 13:47:01 -0700, Nikos <> wrote:

    > @titlelist = <FILE>;


    You don't chomp() it. May that be your problem?

    >i try to print popup_menu( -name=>'title' -values=>\@titlelist ) but
    >it return nothing.


    return?!?

    However there's a syntax error in the above. May that be your problem?

    >if i try to print @titlelist is filled though...


    How do you check that?

    The following seems to work, although it is an insane example:

    #!/usr/bin/perl

    use strict;
    use warnings;
    use CGI qw/:all/;

    print popup_menu -name=>'title',
    -values => do {
    open my $f, '<', $0 or die "D'Oh! $!\n";
    chomp(my @l=<$f>);
    \@l;
    };

    __END__


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 5, 2007
    #13
  14. On 5 May 2007 00:46:16 -0700, Nikos <> wrote:

    >I read it that why i use a backslash before the array but stilll the
    >array remains the same.


    I read it that why... why... WHY?!? What?

    Putting a backslash in front of an array will give you a reference to
    it, but won't modify it in any way. Did you expect it would? Your
    words may be interpreted in this sense. If so, then what gave you that
    idea? What does it have to do with what we were discussing, anyway?
    However I'm not expert at deciphering Linear A scripts. Who is, after
    all?


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 5, 2007
    #14
  15. Nikos <> wrote:
    > On May 5, 1:50 am, Tad McClellan <> wrote:
    >
    >> For those of you playing along at home, Michele is
    >> demonstrating "Use Rule 1" from:

    >
    >
    > I read it



    But not carefully enough...


    > that why i use a backslash before the array



    Errr, that is *Make* Rule 1 not "Use Rule 1".

    "Make Rule 1" is about making a reference. (with a backslash)

    "Use Rule 1" is about dereferencing a reference (with @{} ).

    Which one you use depends on if you want to *make* a reference
    or if you want to *use* a reference (after already having made one).


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 5, 2007
    #15
  16. On Sat, 05 May 2007 13:32:54 +0200, Michele Dondi
    <> wrote:

    >>i try to print popup_menu( -name=>'title' -values=>\@titlelist ) but
    >>it return nothing.

    >
    >return?!?
    >
    >However there's a syntax error in the above. May that be your problem?


    I'm wrong. There's a missing comma, but it doesn't give a syntax error
    as I had naively assumed. "Of course" I realized that when reading the
    discussion originating from the PM node at

    http://perlmonks.org/?node_id=613689

    to which the OP copied and pasted the same question asked here, almost
    verbatim, but for a minimal formatting intervention. Without saying so
    here. Without saying so there.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 5, 2007
    #16
  17. Nikos

    Nikos Guest

    On May 5, 3:30 pm, Tad McClellan <> wrote:
    > Nikos <> wrote:
    > > On May 5, 1:50 am, Tad McClellan <> wrote:

    >
    > >> For those of you playing along at home, Michele is
    > >> demonstrating "Use Rule 1" from:

    >
    > > I read it

    >
    > But not carefully enough...
    >
    > > that why i use a backslash before the array

    >
    > Errr, that is *Make* Rule 1 not "Use Rule 1".
    >
    > "Make Rule 1" is about making a reference. (with a backslash)
    >
    > "Use Rule 1" is about dereferencing a reference (with @{} ).
    >
    > Which one you use depends on if you want to *make* a reference
    > or if you want to *use* a reference (after already having made one).
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas


    i tried agian to post here but i failed.
    Nikos, May 5, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. palmiere
    Replies:
    1
    Views:
    329
    Erwin Moller
    Feb 9, 2004
  2. Robert Kattke

    Populating Select <option>s with form/frame/js

    Robert Kattke, Mar 4, 2004, in forum: Javascript
    Replies:
    1
    Views:
    144
    Robert Kattke
    Mar 7, 2004
  3. Nikos
    Replies:
    3
    Views:
    105
    Nikos
    May 4, 2007
  4. Replies:
    4
    Views:
    112
  5. Populating Select Lists

    , Oct 27, 2006, in forum: Javascript
    Replies:
    2
    Views:
    88
    Stephen Chalmers
    Oct 28, 2006
Loading...

Share This Page