#!/usr/bin/perl
use DBI; use Switch; use CGI qw(:standard);
cgi_header_line(); kings_header();
if ($ENV{'SCRIPT_NAME'} =~ m,\host_update.cgi$,) { mit_header(); standard_body(); }
$db="wotdb";
$host="argon";
$port="33306";
$userid="wot";
$passwd="wot";
$connectionInfo="DBI:mysql:database=$db;$host:$port";
$dbh = DBI->connect($connectionInfo,$userid,$passwd) || die("Could not get connected!");
<---------- Line 23 ----------->
my @ids = ();
my %hostname = ();
$query = "SELECT * FROM hosts, ping_test where hosts.hostname=ping_test.ping_hostname";
$sth = $dbh->prepare($query);
$sth->execute();
my($hostname,$hostip,$hostport);
$sth->bind_columns(\$hostname,\$hostip,\$hostport,\$ping_date,\$ping_time,\$ping_hostname,
\$ping_hostip,\$ping_bytes,\$ping_icmp_seq,\$ping_timems,\$ping_tot_pack_trans,
\$ping_tot_pack_rec,\$ping_perc_pack_loss,\$ping_conn_status);
while(my($id,$hostname)=$sth->fetchrow_array) { push(@ids,$id); $hostname{"$id"} = $hostname; }
print start_form,;
print "<br>";
print "<center><table border=0 cellpadding=3 cellspacing=1>";
print "<tr>";
print popup_menu('name'=>'hostname', 'values'=>[EMAIL PROTECTED],'default'=>$hostname,'labels'=>\%hostname);
print "</tr>";
print "</table></center>";
print "<p><center>";
print qq(<a href=") . "mit.cgi" . '?' . CGI::query_string() . qq(">Back to Main Page</a>\n);
print "</center></p>";
print "</body></html>";
print "<p><center>"; print "You chose to update"; print "</center></p>";
Can't coerce array into hash at(eval 15) line 23
-------------------------------------------------------------
Hi All,
I am trying to fill a popup_menu with values retrieved from my database and get the following error. Can't coerce array into hash at. As this is the first time I'm doing this, could someone please assist in telling me where I'm going wrong.
'at' what? That would help. Perl should be giving you a line number that would help track down where the problem is. I couldn't see anything on a quick glance but then usually I don't have to try and see where the problem is, when Perl tells me.
get
$db="wotdb"; $host="test"; $port="436"; $userid="wot"; $passwd="wot"; $connectionInfo="DBI:mysql:database=$db;$host:$port"; $dbh = DBI->connect($connectionInfo,$userid,$passwd) || die("Could notconnected!");
my @ids = (); my %hostname = ();
$query = "SELECT * FROM hosts, ping_test where hosts.hostname=ping_test.ping_hostname";
Why select * when you could just select what you need?
$sth = $dbh->prepare($query); $sth->execute(); my($hostname,$hostip,$hostport);
Are the other variables already declared?
$sth->bind_columns(\$hostname,\$hostip,\$hostport,\$ping_date,\$ping_time,\$ping_hostname,\$ping_hostip,\$ping_bytes,\$ping_icmp_seq,\$ping_timems,\$ping_tot_pack_trans,
\$ping_tot_pack_rec,\$ping_perc_pack_loss,\$ping_conn_status);
Why are you binding the columns like this and then calling fetchrow_array?
while(my($id,$hostname)=$sth->fetchrow_array) {
Personally to me it is easier to call fetchrow_hashref, then you don't need to bind the columns or worry about a whole bunch of separate variable names. Which ends up looking something like,
while (my $row = $sth->fetchrow_hashref) {
$hostname{ $row->{'id'} } = $row->{'hostname'};
push(@ids,$id); $hostname{"$id"} = $hostname;
Why the two variables? You can use 'keys' with the hash to reproduce the same list below.
perldoc -f keys
}
print start_form,; print "<br>"; print "<center><table border=0 cellpadding=3 cellspacing=1>"; print "<tr>"; print popup_menu('name'=>'hostname', 'values'=>[EMAIL PROTECTED],'default'=>$hostname,'labels'=>\%hostname); print "</tr>"; print "</table></center>";
http://danconia.org
-- Department of Computer Science Support Room 6D, Strand Monday-Friday 09:30-17:00 (020) 7848 1370
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>