awohld has asked for the wisdom of the Perl Monks concerning the following question:
This code takes all the "Distinct" entries in a DB column and puts them in a drop down select HTML form field.
Here is my code:
As you can see from my code I ended up using a "while" statement to fetch each row and then print one at a time. So when I delete "my @market = $market_sth->fetchrow_array; ", since it's code that doesn't do anything, my "while ($market = $market_sth->fetchrow_array)" breaks and nothing prints out.
Even when I rename "my @market = $market_sth->fetchrow_array;" to "my @market_crazy_name = $market_sth->fetchrow_array;" nothing changes!
Why doesn't any of the DB fetch print in the HTML drop down box with the "my @market = $market_sth->fetchrow_array;" code removed? What the HECK!
UPDATE:
When I change "while ($market = $market_sth->fetchrow_array)" to "while (@market = $market_sth->fetchrow_array)" and delete "my @market = $market_sth->fetchrow_array;" everything works!.
Now my question is:
Why does this work?
Here is my code:
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
As you can see from my code I ended up using a "while" statement to fetch each row and then print one at a time. So when I delete "my @market = $market_sth->fetchrow_array; ", since it's code that doesn't do anything, my "while ($market = $market_sth->fetchrow_array)" breaks and nothing prints out.
Even when I rename "my @market = $market_sth->fetchrow_array;" to "my @market_crazy_name = $market_sth->fetchrow_array;" nothing changes!
Why doesn't any of the DB fetch print in the HTML drop down box with the "my @market = $market_sth->fetchrow_array;" code removed? What the HECK!
UPDATE:
When I change "while ($market = $market_sth->fetchrow_array)" to "while (@market = $market_sth->fetchrow_array)" and delete "my @market = $market_sth->fetchrow_array;" everything works!.
Now my question is:
Why does this work?
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 |
Back to
Seekers of Perl Wisdom