Gossamer Forum
Home : General : Perl Programming :

SQL Loop

Quote Reply
SQL Loop
Argh. I've got the following:

Code:
$sth = $dbh->prepare("SELECT * FROM $DB_MYSQL_NAME WHERE id = '$res_id'");
$sth->execute;
$ref = $sth->fetchrow_hashref();
$sth->finish;

To throw the contents of one row into a hash %ref. However, I can't seem to be able to access that hash? I've tried:

Code:
foreach $row_ref (keys %ref}) {
print "$row_ref ";
}

But that doesn't seem to work. Any help would be appreciated? How do I loop through my hash :-\

- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
Its a hashref so you can't use:

foreach $row_ref (keys %ref}) {

....because that's for a normal hash Crazy ....also you have an unmatched bracket Tongue

Try:
Code:
foreach (keys %{$ref}) {
print $ref->{$_};
}

Last edited by:

PaulW: Nov 22, 2001, 6:27 AM
Quote Reply
Re: [PaulW] SQL Loop In reply to
Hi Paul

Thanks for your reply. I've now got the following:

Code:
$sth = $dbh->prepare("SELECT * FROM $DB_MYSQL_NAME WHERE id = '$res_id'");
$sth->execute;
$ref = $sth->fetchrow_hashref();
$sth->finish;

And I want to be able to do this:

Code:
while (($db_input_values_k, $db_input_values_v) = each %ref)
{
$$db_input_values_k = $db_input_values_v;
}

Is there any way that can be done? I guess I need to retireve the values from the database in a different way if I want to do this?

- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
I don't understand what you are trying to do. You only have one row in $ref so you don't need to loop through anything.

Just..

$ref->{Column};
Quote Reply
Re: [PaulW] SQL Loop In reply to
Hi Paul

I need to get all colums in a row into a hash or something. Because I need to assign every column value to it's column name for my Template parser to pick them up. :-)

I was thinking of something like:

Code:
while (($db_input_values_k, $db_input_values_v) = each %ref)
{
$$db_input_values_k = $db_input_values_v;
}

Which doesn't want to work. Do you understand what I'm trying to do? Any way you know I can do it?

- wil
Quote Reply
Re: [PaulW] SQL Loop In reply to
Hi Paul

At the moment, I've got the following code...

Code:
my %db_input_values =
(
name_en => $ref->{name_en},
...
);

while (($db_input_values_k, $db_input_values_v) = each %db_input_values)
{
$$db_input_values_k = $db_input_values_v;
}

But I don't want to explicitly name every one, I want to be able to use a loop to automate this process.

See my above message.

I must be reading the data in from the DB incorrectly ?

- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
How about:

Code:
my %hash;

while (($k,$v) = each %{$ref}) {
$hash{$k} = $v;
}

Quote Reply
Re: [PaulW] SQL Loop In reply to
Code:
my %hash;
while (($k,$v) = each %{$ref})
{
$hash{$k} = $v;
print "$hash{$k}";
}

Does nothing or prints nothing? hmm.

- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
Stop quoting perl code grrrr

I just tested and it works fine.

Code:
$ref = { KEY1 => 'VALUE1', KEY2 => 'VALUE2' };

while (($k,$v) = each %{$ref}) {
$hash{$k} = $v;
print $hash{$k};
}

Maybe you aren't selecting anything from mysql. Thats why you should use $DBI::errstr Tongue
Quote Reply
Re: [PaulW] SQL Loop In reply to
Yes, that code works fine. But it still doesn't want to assign them to their variable name.

How do I find the variable names used then?

- wil
Quote Reply
Re: [PaulW] SQL Loop In reply to
Hi Paul

Yes! Thanks for your help. I've now got the following which is what I was looking for:

Code:
while (($db_input_values_k,$db_input_values_v) = each %{$ref})
{
# $$hash{$db_input_values_k} = $db_input_values_v;
$$db_input_values_k = $db_input_values_v;
}

Sorry if I was unclear in previous messages.

Cheers

- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
Oh crap. It obviousl grabs every single param. How can I ask it not to parse a few params. I've got the following in an array:

Code:
my @db_col_values = ('res_type','res_places_sl','res_places_ue','res_places_re','res_skills_en','res_skills_wo',
'res_skills_ma','res_skills_se','res_skills_ic','res_lang','islive');

How can I tell:

Code:
while (($db_input_values_k,$db_input_values_v) = each %{$ref})
{
$$db_input_values_k = $db_input_values_v;
}

to leave the params in the array alone? :-\

Thanks

- wil

Last edited by:

Wil: Nov 22, 2001, 8:12 AM
Quote Reply
Re: [Wil] SQL Loop In reply to
Sorry you've lost me again.

Why are you using:

$$db_input_values_k
Quote Reply
Re: [PaulW] SQL Loop In reply to
Hi Paul

I'm assigning the variable $db_input_values_v to $db_input_values_k.

If you're assigning the value of one scalar to another you need to use double $$.

What I'm trying to do is to omit some from the hash? Do you know how to do that?

Cheers


- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
>>If you're assigning the value of one scalar to another you need to use double $$.
<<

No you don't. $$ is for referencing.

To skip some hash keys just use something like:

next if $db_input_values_k eq 'whatever';

I still don't get what you are doing. What part does the array play in this and why are you assigning the key to the value?

Last edited by:

PaulW: Nov 22, 2001, 8:56 AM
Quote Reply
Re: [PaulW] SQL Loop In reply to
Ah. Of course. How can I do something like...

next foreach $db_col_value (@db_col_values);

is that possible?

- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
next if grep { /$db_input_values_v/ } @db_col_values;

Last edited by:

PaulW: Nov 22, 2001, 9:02 AM
Quote Reply
Re: [PaulW] SQL Loop In reply to
Hi

It's OK. I've thrown it inside a my foreach loop which I'm using earlier in the script. That snippet didn't work however?

- wil
Quote Reply
Re: [Wil] SQL Loop In reply to
I probably used the wrong variable. I just edited it to:

next if grep { /$db_input_values_v/ } @db_col_values;

Quote Reply
Re: [PaulW] SQL Loop In reply to
Yes, great. Thanks for all your help.

I ended up putting the next; inside a foreach loop in the end because I had opened a foreach loop already and might as well make use of it.

Cheers

- wil
Quote Reply
Re: [PaulW] SQL Loop In reply to
In Reply To:
I probably used the wrong variable. I just edited it to:

next if grep { /$db_input_values_v/ } @db_col_values;

Just out of interest, the above didn't work. But the following did:

Code:
next if grep { /$db_input_values_v/g } @db_col_values;

Cheers

- wil