Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loading a pulldown list from a database

Status
Not open for further replies.

cosports

Programmer
Dec 16, 2000
9
US
I have created a form using Perl/CGI_pm and would like
to load several pulldown list from tables in my MYSQL DB.
Does anyone know a snipit of code that can do this.
Thank you very much
eric
 
use the DBI module
i guess you want something like -

use DBI;

my ($database, $host, $user, $password) =
( "db_name", "localhost", "user_name", "password" );

# connect to database
my $dbh = DBI->connect("DBI:mysql:$database:$host",$user,$password) || die "Unable to connect to database.";


my $statement = "select field from table;";

# prepare statement
my $sth = $dbh->prepare($statement) || die "Can't prepare $statement";

# execute statement
my $rv = $sth->execute || die "can't execute the query";


print "Content-type: text/html\n\n";
print &quot;<html><head></head><body>\n&quot;;
print &quot;<form>\n&quot;;
print &quot;<select>\n&quot;;

while(my @result = $sth->fetchrow_array) {
print &quot;<option>@result[0]\n&quot;;
}

my $rc = $sth->finish;
$dbh->disconnect;

print &quot;</select>\n&quot;;
print &quot;</form>\n&quot;;
print &quot;</body></html>\n&quot;;

 
Excellect...
It was the &quot;While loop&quot; that
stumped me...
Thanks
Eric
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top