
davorg
Thaumaturge
/ Moderator
Sep 19, 2002, 11:53 AM
Post #4 of 4
(629 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.
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.
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.
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.
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
|