Loading ...
Sorry, an error occurred while loading the content.

[PBML] Re: CGI.pm & DBD::mysql Question

Expand Messages
  • Jeff Boes
    ... If the select statement returns just one column, you can do it like this: [note: some preparation steps and most error handling omitted for clarity] $stmt
    Message 1 of 2 , Jan 24, 2000
    • 0 Attachment
      > -----Original Message-----
      > From: Aram Mirzadeh [mailto:awm@...]
      > Sent: Friday, January 21, 2000 3:00 PM
      > To: perl-beginner@...
      > Subject: [PBML] CGI.pm & DBD::mysql Question
      >
      >
      >
      > Hi,
      >
      > I'm trying to load a select statement into a popup_menu for
      > displaying on a
      > website.
      >
      > Using any of the normal functins, the only thing i can return are either
      > single item at a time, or an array of the row, or the entire data tree.
      >
      > +------+
      > | list |
      > +------+
      > | abc1 |
      > | abc2 |
      > | abc3 |
      > +------+
      >
      > I have tried using hash to get the value into the CGI.pm
      > popup_menu but that
      > doesn't seem to like it.

      If the select statement returns just one column, you can do it like this:

      [note: some preparation steps and most error handling omitted for clarity]

      $stmt = $dbh->prepare(q!SELECT list FROM mytable WHERE...!);
      print popup_menu(-name=>'menu',
      -values=>[ map($_->[0],
      @{$stmt->fetchall_arrayref} ]);

      This is pretty terse, so let me expand it a little.

      @listrefs = @{$stmt->fetchall_arrayref};

      Now @listrefs is an array, where each element is itself an array reference.
      For example, $listrefs[0] is the reference pointing to a list which contains
      the first row returned by $stmt. So $listrefs[0][0] is the first value in
      the first row.

      @list = map($_->[0], @listrefs);

      This would "un-indirect" the listrefs array, returning all the values as a
      "normal" list. Now $list[0] is the same as $listrefs[0][0].

      The -values parameter to CGI calls wants a reference to an array, so we
      construct it with [...] above.

      I don't have a MySQL installation handy to test this code, but I think it's
      correct as it corresponds with things I've done in similar situations.


      ----
      The whole problem with the world is that fools and fanatics are always
      so certain of themselves, and wiser people so full of doubts.
      --Bertrand Russell
    Your message has been successfully submitted and would be delivered to recipients shortly.