# Listing 1: select-list-from-table.pl

#!/usr/bin/perl -wT
# filename: select-list-from-table.pl

use strict;
use diagnostics;
use DBI;

# Change these for your own system
my $database = "test";
my $username = undef;
my $password = undef;

# Connect to the database
my $dbh = DBI->connect("DBI:mysql:$database:localhost",
		       $username, $password) ||
    die qq{DBI error from connect: "$DBI::errstr"};

# Create the SQL query
my $sql = "SELECT id, name ";
$sql .= "FROM RailStations ";
$sql .= "ORDER BY name ";

# Prepare the query
my $sth = $dbh->prepare($sql) 
    || die "Cannot prepare: $DBI::errstr";

# Execute the query
my $success = $sth->execute;

# Iterate through the results
while (my $row_ref = $sth->fetchrow_arrayref)
{
    my ($id, $name) = @$row_ref;
    print qq{<option value="$id">$name\n};
}

$sth->finish;
$dbh->disconnect;

