Some things to consider:
- If @compile has several items with the same value, then you have a duplicate.
- if $identifier is duplicate in your table, then it is printed more than once.
- You have @version in your query, but it does not seem to be declared. Moreover, @version is an array. Are you sure you want to use an array as a single value? Should it be $version instead?
- "SELECT *" will get all the fields, without a guarantee of a particular order. Then your $identifier will get the first column in
your result set, whichever it is. If you need only one column, name it in your SELECT.
- Perl has several ways of quoting strings. Using qq{}, qq(), qq[],qq<> will let you use double quotes as literals inside
your string.
- Better yet, use $dbh->quote to quote your variables before merging them in a query string.
- Even better yet, use placeholders. See below for an example and DBI Recipes for a wider explanation.
- The error string for DBI comes with $DBI::errstr, not "DBI:: error"
Here is an (untested) rewrite of your code.
Make sure you understand what is there. See our Tutorials page for more basics on database programming with Perl.
my @compile=param ('compile');
my $version = "whatever";
my $dbh=DBI->connect($dsn,$user,$password,)
or die "Error $DBI::errstr connecting to $dsn";
my $sth=$dbh->prepare(qq{
SELECT
mycolumn
FROM
ddts
WHERE
`Stat`= ?
AND `Project`= ?
AND `Version`= ?
});
$sth->execute("T", "Def", $version);
print "<table border=2>";
while (my ($identifier) = $sth->fetchrow_array ) {
foreach my $elem (@compile) {
if ($elem eq $identifier) {
print "<tr><td>$identifier</td></tr>";
}
}
}
print "</table>";