Populating an array from a mysql select

N

Nikos

$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.
 
M

Mumia W.

$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};
 
N

Nikos

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?
 
G

Gary E. Ansok

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
 
N

Nikos

Nikos said:
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?!
 
M

Michele Dondi

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
 
M

Michele Dondi

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
 
N

Nikos

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?
 
M

Michele Dondi

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
 
N

Nikos

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...
 
T

Tad McClellan

Michele Dondi said:
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
 
N

Nikos

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.
 
M

Michele Dondi

@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
 
M

Michele Dondi

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
 
T

Tad McClellan

Nikos said:
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).
 
M

Michele Dondi

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
 
N

Nikos

But not carefully enough...


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).

i tried agian to post here but i failed.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top