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#
######################
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.