Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Populating an array from a mysql select

Reply
Thread Tools

Populating an array from a mysql select

 
 
Nikos
Guest
Posts: n/a
 
      05-04-2007
$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.

 
Reply With Quote
 
 
 
 
Mumia W.
Guest
Posts: n/a
 
      05-04-2007
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};


 
Reply With Quote
 
 
 
 
Nikos
Guest
Posts: n/a
 
      05-04-2007
On May 4, 6:58 pm, "Mumia W." <paduille.4061.mumia.w
(E-Mail Removed)> 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?


 
Reply With Quote
 
Gary E. Ansok
Guest
Posts: n/a
 
      05-04-2007
In article <(E-Mail Removed) .com>,
Nikos <(E-Mail Removed)> 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
 
Reply With Quote
 
Nikos
Guest
Posts: n/a
 
      05-04-2007
On May 4, 10:15 pm, (E-Mail Removed) (Gary E. Ansok) wrote:
> In article <(E-Mail Removed) .com>,
>
> Nikos <(E-Mail Removed)> 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?!

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-04-2007
On Fri, 4 May 2007 19:15:32 +0000 (UTC), http://www.velocityreviews.com/forums/(E-Mail Removed)
(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,
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-04-2007
On 4 May 2007 12:21:36 -0700, Nikos <(E-Mail Removed)> 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,
 
Reply With Quote
 
Nikos
Guest
Posts: n/a
 
      05-04-2007
On May 4, 10:43 pm, Michele Dondi <(E-Mail Removed)> wrote:
> On 4 May 2007 12:21:36 -0700, Nikos <(E-Mail Removed)> 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?

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-04-2007
On 4 May 2007 12:59:44 -0700, Nikos <(E-Mail Removed)> 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,
 
Reply With Quote
 
Nikos
Guest
Posts: n/a
 
      05-04-2007
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...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating an array from a mysql select Nikos Perl Misc 3 05-04-2007 07:16 PM
Populating Select Lists bryan.christina@gmail.com Javascript 2 10-28-2006 02:00 AM
Populating a SELECT list with HTTPRequest bobsawyer@gmail.com Javascript 4 02-04-2005 03:58 PM
Populating Select <option>s with form/frame/js Robert Kattke Javascript 1 03-07-2004 03:08 AM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM



Advertisments