unable to make SELECT take variables
From: John S Brigham (mrphysh_at_juno.com)
Date: 11/03/03
- Next message: Jeff Zucker: "Re: unable to make SELECT take variables"
- Previous message: Tim Bunce: "Re: Problems compiling DBI 1.38 on perl 5.6.1"
- Next in thread: Jeff Zucker: "Re: unable to make SELECT take variables"
- Reply: Jeff Zucker: "Re: unable to make SELECT take variables"
- Reply: Hardy Merrill: "Re: unable to make SELECT take variables"
- Maybe reply: Jeffrey Seger: "Re: unable to make SELECT take variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: dbi-users@perl.org Date: Mon, 3 Nov 2003 07:34:26 -0800
I need help.
I am working on a MySQL /Perl web site. I am teaching myself MySQL and
PERL and am making progress.
As an exercise, I want to put my Mother's Christmas card list in a MySQL
database. I want to sort and print the mailing labels from the database.
This is all done in PERL.
The machinery is all in place. I have built and am now filling the
database with names and addresses.
I want to be able to query the database for specific records through an
interactive Perl script. With some effort, I have gotten the SELECT
statement to work but is will not take a string variable.
Short question:
$cty = <STDIN>;
chomp $cty;
#then
.......SELECT column from table where City = $cty #doesn't work.
No matter how I do it, the script will not take the variable. There is
no error. variable causes an error
.......................
These work:
.................................................
my $sth = $dbh->prepare(q {
SELECT Full_Name ,Street_Address
FROM momaddr
WHERE City = "Denver"# The table,column names are not case specific
}) or die "can't prepare statement"; #the double quotes around column
specifics are required
$sth->execute()
or die "can't execute";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME}}\n\n\n";
while (($Full_Name,$Street_Address) = $sth->fetchrow_array) {
print "$Full_Name$Street_Address\n\n";
}
# check for problems which may have terminated the fetch early
die $sth->errstr if $sth->err;
............................................................
these do not work: (No error but the statement will not take the variable
$cty)
......................................................
my $cty = "Denver";
my $sth = $dbh->prepare(q {
SELECT Full_Name ,Street_Address
FROM momaddr
WHERE City = "$cty"# The table,column names are not case specific
}) or die "can't prepare statement"; #the double quotes around column
specifics are required
$sth->execute()
or die "can't execute";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME}}\n\n\n";
while (($Full_Name,$Street_Address) = $sth->fetchrow_array) {
print "$Full_Name$Street_Address\n\n";
}
System:
>
> Windows 2000
> pentium II
> PERL Active Perl version 1.37 5.8.0.806
> MySQL version 2000 version 5.00.2195
>
> Thanks in advance
> John in Denver, Colorado USA
>
> _____
________________________________________________________________
The best thing to hit the internet in years - Juno SpeedBand!
Surf the web up to FIVE TIMES FASTER!
Only $14.95/ month - visit www.juno.com to sign up today!
- Next message: Jeff Zucker: "Re: unable to make SELECT take variables"
- Previous message: Tim Bunce: "Re: Problems compiling DBI 1.38 on perl 5.6.1"
- Next in thread: Jeff Zucker: "Re: unable to make SELECT take variables"
- Reply: Jeff Zucker: "Re: unable to make SELECT take variables"
- Reply: Hardy Merrill: "Re: unable to make SELECT take variables"
- Maybe reply: Jeffrey Seger: "Re: unable to make SELECT take variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|