awohld has asked for the wisdom of the Perl Monks concerning the following question:
Originally I had "my @market = $market_sth->fetchrow_array;" in there because I wanted to store the entire DB fetch in an array, then print it. I didn't know what I was doing and this is wrong.#!/usr/local/bin/perl -w use strict; use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); my $q = CGI->new(); print $q->header(); ##Start database connections my $database = "database"; my $db_server = "localhost"; my $user = "user"; my $password = "password"; ##Connect to database, insert statement, & disconnect my $dbh = DBI->connect( "DBI:mysql:$database:$db_server", $user, $pass +word); my $market_stmt = "SELECT DISTINCT market FROM table ORDER BY market A +SC"; my $market_sth; $market_sth = $dbh->prepare($market_stmt) or die "Couldn't prepare the + query: ".$DBI::errstr; $market_sth->execute() or die "Couldn't execute query: ".$DBI::errstr; my @market = $market_sth->fetchrow_array; #This is some old code wher +e I tried to fetch all rows and store in an array, this should be del +eted, I didn't know what I was doing! When deleted, nothing prints i +n the "while" statement below! print <<EOF; <select name="market" size="1"> EOF while (my $market = $market_sth->fetchrow_array) { print "<option value=\"$market\">$market</option>" ; } print <<EOF; </select> EOF
And this doesn'tmy @market712 = $market_sth->fetchrow_array; while (my $market = $market_sth->fetchrow_array) { print "<option value=\"$market\">$market</option>" ; }
What's so special about assigning the first $market_sth->fetchrow_array to @any_array, that it will later let me fetch each row to a scalar?# my @market712 = $market_sth->fetchrow_array; while (my $market = $market_sth->fetchrow_array) { print "<option value=\"$market\">$market</option>" ; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Mysterious Code "DBI" - Why is this working?
by graff (Chancellor) on Jun 09, 2005 at 03:14 UTC | |
Re: Mysterious Code "DBI" - Why is this working?
by cees (Curate) on Jun 09, 2005 at 05:07 UTC | |
Re: Mysterious Code "DBI" - Why is this working?
by hubb0r (Pilgrim) on Jun 09, 2005 at 05:58 UTC |