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

    Henry Law Guest

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


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


    fetchrow_array fetches the _fields_ from one row of your result set as a
    list; in this case it's a one-element list containing the username. You
    need to code a loop, getting each row of the result set in turn and
    handling it.

    Like this (untested and almost certainly broken)

    while (my @userlist = $select->fetchrow_array ) {
    popup_menu( -name=>'select', -values=> $userlist[0] );
    }

    --

    Henry Law Manchester, England
     
    Henry Law, May 4, 2007
    #2
    1. Advertising

  3. In article <>,
    Nikos <> wrote:
    >$select = $db->prepare( "SELECT username FROM users" );
    >$select->execute;
    >my @userlist = $select->fetchrow_array;
    >
    >In the above code insteaf od the array filling by all usernmes
    >selected from the dataabse it only populates the 1st one.


    You might want to look at the function selectcol_arrayref()
    rather than doing the prepare, execute, and fetch separately.

    my $userlist = $db->selectcol_arrayref("SELECT username FROM users");
    # now @$userlist should be an array containing the user names

    On a more general note, my usual way of doing database queries
    (assuming the expected number of rows is not too large, so that
    holding all the data in memory is not a problem) has become

    my $aref = $db->selectall_arrayref($sql, { Slice => {} }, @values);

    foreach my $row (@$aref) {
    # do something with $row->{col1_name}, $row->{col2_name}, etc.
    }

    Gary Ansok
    --
    Any attempt to brew coffee with a teapot should result in the error code
    "418 I'm a teapot". The resulting entity body MAY be short and stout.
    - RFC 2324, Hyper Text Coffee Pot Control Protocol (HTCPCP)/1.0
     
    Gary E. Ansok, May 4, 2007
    #3
  4. Nikos

    Nikos Guest

    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( 'ÐÝò ìïõ ôé èá Þèåëåò:' ),
    td( popup_menu( -name=>'title' -values=>\@titlelist ))),
    Tr( td( 'ÊÜôé Üëëï ðïõ èá Þèåëåò íá ó÷ïëéÜóåéò?' ),
    td( textarea( -name=>'remark', -rows=>4, -columns=>25 ))),
    Tr( td( 'Ôï ôçëÝöùíï åðéêïéíùíßáò óïõ ãéá åðéâåâáßùóç
    åßíáé:' ), td( textfield( -name=>'phone' ))),

    Tr( td( a( {href=>'/cgi-bin/show.pl?name=showbook'},
    font( {size=>3, color=>'yellow'}, 'ÅìöÜíéóç!' ))),
    td( submit( 'ÁðïóôïëÞ!' )))
    );


    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!!
     
    Nikos, May 4, 2007
    #4
    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:
    471
    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:
    163
    Robert Kattke
    Mar 7, 2004
  3. Nikos
    Replies:
    16
    Views:
    225
    Nikos
    May 5, 2007
  4. Replies:
    4
    Views:
    134
  5. Populating Select Lists

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

Share This Page