http://qs321.pair.com?node_id=201039

RayRay459 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, i am in dire need of your assistance. I have a mysql database which has names and a default value for ip addresses. I have other code , a cgi app that uses a form to add/search the database and also view all current items in the database which still seems to be working. i am trying to write a script that will take the names, look them up and populate the database with the correct ip address. When i run my script i get a really wierd error.
DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at lookup.pl line 34.
If anyone can offer some advice as too why my code is failing, i would greatly appreciate it. Thank you
Ray
#!/usr/bin/perl -w # lookup.pl - This will take the name from the database and lookup the + ip address and populate it in the database. # Ray Espinoza # version 1.0 9/26/02 ########################################################## use strict; use DBI; # Create database handle ######################## my $dbh = &connect; get_name_and_lookup ($dbh); # Close connection to the database and end html ############################################### $dbh->disconnect(); # Subs ############################################################### +############ sub get_name_and_lookup { my $dbh = shift; my ($sth, $stmt, $hostname, $id, $addr, $ipaddr); $stmt = qq {SELECT * from nt_machines ORDER BY name}; $sth = $dbh->prepare ($stmt); $sth->execute(); while (my $row = $sth->fetchrow_hashref()) { $row->{name} = $hostname; $row->{id} = $id; $addr = (gethostbyname($hostname))[4]; if ($addr) { $ipaddr = join(".", unpack("C4", $addr)); update_item($ipaddr,$id) } $sth->finish(); } } ###################################################################### +##### sub update_item { my ($dbh, $ipaddr, $id) = @_; $ipaddr =~ s/^\s+//; $ipaddr =~ s/\s+$//; $dbh->do (qq{ UPDATE nt_machines SET ipaddr = ? WHERE id = ? } +, undef, $ipaddr, $id) or warn "Can't update database: $!\n"; } ###################################################################### +##### sub connect { use DBI; my ($dbh, $sth, $count); $dbh= DBI->connect("DBI:mysql:host=localhost;database=testsites","qaus +er","blah",{PrintError => 0, RaiseError => 1}); return $dbh; } ###################### # Ray Espinoza 9.26.02# ######################