You are putting your $sth in a lexical scope. It might also be better to
define the statement as a variable, then prepare it - just a thought, but
here it is:


my $dbh = DBI->connect("DBI:mysql:$database:$hostname", $user,
$datpassword);
my $select;

if ($contractor_id eq "") {
$select  = qq{select blah1, blah2 from contractors where (username =
'$username') and
(password = '$password')
 };

 } #end if not contractor id

else {

$select = qq{select blah1, blah2 from ap_contractors where (id =
'$contractor_id')
 };

 } #end else
my $sth = $dbh->prepare($select);
$sth->execute();

When you use "my" to declare a variable within a lexical scope (basically,
in a block of code surrounded by curly brackets), that variable goes out of
scope outside the brackets. You used my within the if {} else{} so $sth is
out of scope outside that if statement. Declare the variable outside the
brackets, then modify it inside the brackets.

Steve H.

-----Original Message-----
From: Greg Thompson [mailto:[EMAIL PROTECTED]]
Sent: Monday, October 22, 2001 8:36 PM
To: [EMAIL PROTECTED]
Subject: Quick Question


Hi, I'm new to the list and had a quick question regarding selection using
if statements. It would be much appreciated if anyone could point me in the
right direction.
I'm new to using Perl with MySQL and was wondering why it will not allow me
to do the following:
my $dbh = DBI->connect("DBI:mysql:$database:$hostname", $user,
$datpassword);

if ($contractor_id eq "") {
my $sth = $dbh->prepare(qq{
    select blah1, blah2 from contractors where (username = '$username') and
(password = '$password')
 });

 } #end if not contractor id

else {

my $sth = $dbh->prepare(qq{
    select blah1, blah2 from ap_contractors where (id = '$contractor_id')
 });

 } #end else
$sth->execute();

I know this is probably simple, but I do not know why it does not perform
the selection based on my if statements. I've tried selecting stuff using
other if statements, and they did not work either. But once I take away the
if/else statement, it works fine, but then the problem remains that I need
to select different things based on if somethings true (which in the example
I gave above, the condition is if $contractor_id has a value or not).
Any input would be much appreciated.
Thanks,
Greg

Reply via email to