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>";
| [reply] [d/l] [select] |
I highly suspect that the error is with your foreach $elem(@compile) line. Stick use Data::Dumper to your code, and check the values of your @compile.
use Data::Dumper;
my @compile ...
print "<pre>\n", Dumper(\@compile), "\n</pre>\n";
Another possibility is that your table has duplicate data. To check for this, simply insert a print after the while loop -
while (my($identifier)=...) {
print "<tr><td><font color=#FF0000>", # <- insert print
"$identifier</font></td></tr>\n";
foreach ...
}
| [reply] [d/l] [select] |
I found the problem: i had had duplicate values in @compilea from my mistake.Now it's printing ok.
Thank you very much for your help.
| [reply] |
- @compile can contain anything, so you should really quote its value(s) when you use them in the prepare call (quote as in DBI->quote)
- $identifier can contain anything (you cannot rely on it returning column values in the order you expect).
Be specific in your selects, don't select information you're never going to use (don't "select *" when all you don't want it)
| [reply] |