CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Why database data is wrong ? hash data

 



Jack
Novice

Sep 14, 2002, 12:53 PM

Post #1 of 4 (441 views)
Why database data is wrong ? hash data Can't Post

Hi,

I have some code like this.

while (@tempLo and @tempID){
my %res;
$res{LO}=shift@tempLo;
$res{ID}=shift@tempID;
push @listingOrder, \%res;
}
$ERR = db::update_order($dbh, @listingOrder) ;


Here is db::update_order:

sub update_order ($$) {
my $dbh=shift @_;
my @tempArray=@_;
while (@tempArray) {
my
$tmp=shift@tempArray;
my $lo=%$tmp->{'LO'};
my $id=%$tmp->{'ID'};

my $sqlStatement = "UPDATE Listings set lo =? where ID=?";
my $sth = $dbh->prepare($sqlStatement);
$sth->execute($lo,$id);
}


but when I get it from database, it will display something like this,

HASH(0x839a4f4)

Here is the code I used to read data from database.

my $row = $sth->fetchrow_hashref) {
$row->{LO} = $row->{LO}


}

Can you tell me why?

thanks


Paul
Enthusiast

Sep 14, 2002, 1:12 PM

Post #2 of 4 (438 views)
Re: [Jack] Why database data is wrong ? hash data [In reply to] Can't Post

>>
$row->{LO} = $row->{LO}
<<

That is doing nothing, but in answer to your question, you are using fetchrow_hashref which is returning a hashref, simple as that.

Please go and read the dbi docs.

http://www.perldoc.com/perl5.6.1/lib/DBI.html

In future (as other moderators have requested three times now), please post replies relating to the same code, in the same thread and not duplicates.


(This post was edited by RedRum on Sep 14, 2002, 1:13 PM)


Jack
Novice

Sep 19, 2002, 10:55 AM

Post #3 of 4 (370 views)
Re: [RedRum] Why database data is wrong ? hash data [In reply to] Can't Post

Hi,
it still doesn't work? can you give me more hint?
thanks


davorg
Thaumaturge / Moderator

Sep 19, 2002, 11:53 AM

Post #4 of 4 (366 views)
Re: [Jack] Why database data is wrong ? hash data [In reply to] Can't Post

There is so much going wrong with your code that it's hard to know where to start. You should really read a good introductory Perl book like Learning Perl or Elements of Programming with Perl.

It would also make your code eaier to follow of you used "code" tags rather that the eye-straining colours and bad formatting that you currently use.


Code
while (@tempLo and @tempID) { 
my %res;
$res{LO} = shift @tempLo;
$res{ID} = shift @tempID;
push @listingOrder, \%res;
}
$ERR = db::update_order($dbh, @listingOrder) ;

So by the end of this code, you have an array called @listingOrder which contains references to hashes. Each hash has two keys - LO and ID.


Code
sub update_order ($$) { 
my $dbh = shift @_;
my @tempArray = @_;
while (@tempArray) {
my $tmp = shift @tempArray;
my $lo = %$tmp->{'LO'};
my $id = %$tmp->{'ID'};

my $sqlStatement = "UPDATE Listings set lo =? where ID=?";
my $sth = $dbh->prepare($sqlStatement);
$sth->execute($lo,$id);
}
}


It all starts to go wrong here. Why does update_order have a prototype of $$. That forces both arguments into scalar context. That's not a problem for the first argument as that already is a scalar, but your second argument is an _array_. When you force it into scalar context (as your prototype does) you don't get any of the data in the array - you simply get the number of elements in an array. Perl prototypes are pretty complex for beginners to understand and they are usually unnecessary. You're better off not using them at all here.

The upshot of all this is that when you assign a value to @tempArray, you get an array with one element - the number of elements in your previous array. This is a scalar _not_ a hash reference as your code almost expects. I say "almost" because you have unnecessary '%' characters in your expressions. Had you been running with "use warnings" turned on you would have got warnings about trying to use a scalar as a hash reference.

So your $lo and $id variables both end up containing the value "undef" because of your illegal hash lookups.

Also, why do you prepare the SQL statement each time round the loop? The whole point of allowing placeholders in SQL is that you can prepare it once (saying time) and execute it many times. That's not a bug, but it is a pretty stupid thing to do.


In Reply To
but when I get it from database, it will display something like this,

HASH(0x839a4f4)


See, we've got no idea what you're doing to print that. You haven't shown us the code that display that. All we know is that you're printing a hash reference.


Code
my $row = $sth->fetchrow_hashref) { 
$row->{LO} = $row->{LO}


This is so weird, it's hard to know what you were trying to do. You read a hash of data from the database and then you overwrite one of the values in the hash with itself. That does nothing but waste time. What do you think this code does?

Sorry if this sounds a bit harsh but not only was your code badly thought out, your question was also impossible to understand. I've done what I can. Please read some good documentation before writing any more Perl.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives