in reply to MySQL Error

You want something like this:
if (defined $status) { $statement = qq(SELECT COUNT(*) FROM name WHERE status = $status); }else{ $statement = qq(SELECT COUNT(*) FROM name WHERE age = "4"); }

Then call your statement like this:
$sth = $dbh->prepare($statement); $sth-> execute();

Replies are listed 'Best First'.
Re: Re: MySQL Error
by Caillte (Friar) on Nov 20, 2001 at 21:29 UTC

    This isn't perfect as you do not quote $status. It is often better to let DBI do that for you:

    my $arg; if(defined $status) { $statement = 'SELECT COUNT(*) FROM name WHERE status=?;'; $arg = $status; } else { $statement = 'SELECT COUNT(*) FROM name WHERE age=?;'; $arg=4; } $sth = $dbh->prepare($statement) or die "Cannot Prepare $DBI::errstr"; $sth->execute($arg) or die "Cannot execute $DBI::errstr";

    Calling the statement this way makes sure the data is properly quoted.

    $japh->{'Caillte'} = $me;