Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

About perl, doing SQL with an array

Quote Reply
About perl, doing SQL with an array
Normally i use something like:

Code:

my $links_db=$DB->table ('Links', 'CatLinks', 'Category');
my $cond = GT::SQL::Condition->new(
'isValidated' => '=' => 'Yes',
'something' => '=' => '5',
);

$links_db->select_options ("GROUP BY Links.ID ORDER BY Mod_Date DESC", "LIMIT 30");
my $sth2 = $links_db->select( ['Links.ID','Title','URL'] => $cond );

while (my ($id,$title,$url) = $sth2->fetchrow_array) {
...
}


But for now i would like to have everything in an array.

So i can do:
Code:
my @ar;
...
while (my ($ar[ID],$ar[Title],ar) = $sth2->fetchrow_array) {[/code]


But i would like to have something like:
[code]my @ar;
...
while (my ($ar[]) = $sth2->fetchrow_array) {[/code]

Is this possible?

Or is there another way to have an array back without write every field in the code?
I have a lot of fields and i have a lot of selects. Because of this i try to write a sub and pass all the values to this sub.

I would like to have something like:

[code]
select * from ...
while $array = fields {

call sub ($array)

}
[/code]
With this i would save one million lines of code in my build.pm :)

Last edited by:

Robert: Mar 14, 2014, 6:54 AM
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
Found in nph-build something like:

Code:
my $sth = $rel->select('Links.*', @cat_cols, 'CategoryID' => $cond);
last unless $sth->rows;
while (my $link = $sth->fetchrow_hashref) {
...
}

Maybe this will show me the way ...
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
Hi,

I'm a little confused as to what you're after :) Why an array? When I know a bit more about why you're trying to do it, I can give an answer =)

Just quick - why not just a hashref?

Code:
while (my $vals = $sth2->fetchrow_hashref) {
... $vals->{Title}, $vals->{ID} etc. ... based on what you asked it to grab
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] About perl, doing SQL with an array In reply to
I dont speak no single word of perl, i am just like a child playing with Lego. :)

Code:
my @link;

while (@link = $sth->fetchrow_array) {

$output = subname(@link);

}

Code:
sub subname {
my @array = @_[0];
...
return $lala;
}
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
Hi,

Robert wrote:
Found in nph-build something like:

Code:
my $sth = $rel->select('Links.*', @cat_cols, 'CategoryID' => $cond);
last unless $sth->rows;
while (my $link = $sth->fetchrow_hashref) {
...
}

Maybe this will show me the way ...

Ya, all thats doing is grabbing all the fields you want - and putting them into a hash(ref). This just means you can use $val to grab, but then $val->{Foo} to actually get that field. This means you don't have to manually enter every single field you are trying to grab :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
Hi,

Quote:
I dont speak no single word of perl, i am just like a child playing with Lego. :)

hehe NP =)

Sounds like you would indeed be better with fetchrow_hashref. Something like:

Code:
my $links_db = $DB->table ('Links', 'CatLinks', 'Category');
my $cond = GT::SQL::Condition->new( 'isValidated' => '=' => 'Yes', 'something' => '=' => '5' );

$links_db->select_options ("GROUP BY Links.ID ORDER BY Mod_Date DESC", "LIMIT 30");

my $sth2 = $links_db->select( ['Links.ID','Title','URL'] => $cond ) || die $GT::SQL::error;

while (my $hit = $sth->fetchrow_hashref) {
# you now have access to $hit->{ID}, $hit->{Title} and $hit->{URL}
do_stuff($hit);

# $hit will now have the values, including what was editing in do_stuff()
}

sub do_stuff {
# access $hit via $_[0]->{Field}, as this means you dont need to return anything (makes it quicker than doing a copy + return)
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] About perl, doing SQL with an array In reply to
I thought to have it, but
all my $hits['ID'], $hits['Title'] or $hits[0], $hits[1] has the value of the first col (ID).

With $hits->{'ID'} i got an error.
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
What code are you trying?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] About perl, doing SQL with an array In reply to
Code:
my $cond = GT::SQL::Condition->new(
'isValidated' => '=' => 'Yes');

$links_db->select_options ("GROUP BY Links.ID ORDER BY Mod_Date DESC", "LIMIT 30");
my $sth = $links_db->select( ['Links.ID','Title','URL'] => $cond );

while (my $link = $sth->fetchrow_array) {

print $link->{'ID'};

}

It is empty. :(
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
Code:
while (my $link = $sth->fetchrow_array) {

Should be:

Code:
while (my $link = $sth->fetchrow_hashref) {

:)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] About perl, doing SQL with an array In reply to
YOU MADE MY DAY

Laugh

Thank you.
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
And you decrease my build.pm from almost 70 to 40KB. :)
Quote Reply
Re: [Robert] About perl, doing SQL with an array In reply to
=)