norbdum
norbdum

Reputation: 2491

Populating drop-down list from MySQL using Perl

I am trying to populate the drop-down list from mysql data and got the solution from Populating a Drop-Down list using DBI and How do I get selected value from drop down box in Perl CGI

I have following code:

#!/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>
];

At second comment in code I am not able to get the value of @tables for the drop-list. Why?

Upvotes: 0

Views: 2020

Answers (2)

Toto
Toto

Reputation: 91488

my @tables=$dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');

returns an array_ref, in order to use it, you have to dereference it:

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>~;
}

Upvotes: 2

mpapec
mpapec

Reputation: 50657

selectcol_arrayref returns array ref, so:

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>};
}

Upvotes: 2

Related Questions