我正在尝试从mysql数据填充下拉列表,并从Populating a Drop-Down list using DBI获得解决方案 How do I get selected value from drop down box in Perl CGI
我有以下代码:
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use Data::Dumper;
print "Content-type: text/html\n\n";
my $dbh = DBI->connect();
my @tables=$dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');
print Dumper@tables; #this dumper is giving results
print qq[
<!DOCTYPE html>
<html>
<head></head>
<body>
<form id="upload-form"><table>
<tr><td>Table Name:</td><td><select name="tbname">
];
print Dumper@tables; # this dumper is not printing anything
foreach my $table(@tables)
{
print qq "<option value=\"$table\">" . $table . "</option>";
}
print qq[
</select>
</td></tr>
</table></form></body>
</html>
];
在代码中的第二条评论中,我无法获得下拉列表的@tables值。 为什么?
答案 0 :(得分:2)
selectcol_arrayref
返回数组引用,所以:
my $tables = $dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');
foreach my $table (@$tables) {
print qq{<option value="$table">$table</option>};
}
答案 1 :(得分:2)
my @tables=$dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');
返回一个array_ref,为了使用它,你必须取消引用它:
my $tables=$dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');
foreach my $table (@$tables) {
print qq~<option value="$table">$table</option>~;
}