I am connecting to a database and I am taking an input from STDIN that I use as part of an execution.
So I have:
my $i = 0;
while($i != 1) {
print "Input: ";
my $input = <STDIN>;
chomp $input;
my $test = $dbh->prepare("show tables like $input");
and then I want to check that the input is a valid entry in the database and loop round again if it isn’t:
if ($test->execute()) {
print "Input exists in database\n";
$i = 1;
}
else {
print "Input does not exist.\n";
}
} # end of while
I know that this does not work, but I would like something similar that isn’t execute or die as I do not want to exit my program. Is this possible?
I actually found the solution I wanted a different way, but thanks to pmakholm for answering.
What I didn’t think of is that even if I enter nonsense into the database, I’m looking to see if that table exists – and if it doesn’t it just returns an empty set, so I can check if is zero (checking if
NULLor similar may be better but this works). I thought that it would return an error, but it doesn’t. pmakholm – I shall use your method if I need to check that query works, so thanks.