Populate a select tag from MySQL database -
12-15-2003
, 08:38 AM
I'm quite new to HTML writing and MySQL, but am learning quickly. I
currently have some code as follows:
Query sequence:
<select name="query" size=1>
<option selected> All </option>
<option> OFSaCe0004 </option>
<option> OFSaCe0157 </option>
<option> OFSaCe0192 </option>
</select>
which i've been using to create a search page to query a MySQL database
i've setup. The options listed in the example are only a small subset of
those that are present in the database, and i would like to know to populate
the <select> tag with options obtained from a MySQL database. Am i able to
do this with a perl (CGI) script, or do i need (or is it easier) to use PHP
(which i also know nothing about)?
The following mysql query will produce an array (@options) that contains all
the appropriate options i wish to use:
my $dsn = "DBI:mysql:database=$db";
my $dbh = DBI->connect($dsn, $user, $password) or die "Cannot connect to
server: $!\n";
my $sth = $dbh->prepare("SELECT search.seq_id, sequence.local_id FROM
search, sequence WHERE search.seq_id=sequence.seq_id GROUP BY seq_id ORDER
BY local_id");
$sth->execute();
my @options;
while ( my $ref = $sth->fetchrow_hashref() ) {
push @options, $ref->{local_id};
}
Thanks for any help.
Nath |