Huub <> wrote:
> $record = 101;
> $sql = "select voorvoeg,naam from hvw where lidnr = $record";
> $sth = $dbh->prepare($sql);
> $sth->execute or die "SQL Error: $DBI::errstr\n";
> @row = $sth->fetchrow_array;
> print "Naam: @row\n";
>
> This works OK. However, when I put the lines "$sql = " and further in a
> (while-)loop
If you are having a problem with code that contains a while loop,
then you should post code that contains a while loop.
We cannot (generally) debug code that we cannot see.
> it doesn't do anything. Is this correct?
That depends on _why_ you put those lines in a loop. (and how
you coded the loop)
If you want to get the 1st result from different queries (ie. you
plan to have multiple values for $record), then putting those lines
in a loop is correct.
If you want to get all of the results from one particular query,
then you would put only the fetchrow_array() and print() parts
in a loop.
> Also I have tried to read all database fields at once, but that didn't
> work either,
How did you try to do it?
We cannot (generally) debug code that we cannot see.
> since "prepare" seems only to accept 1 parameter.
That is fine, since 1 parameter is all you need:
$sql = "select * from hvw where lidnr = $record";
> How can I
> do this effectively/efficiently?
You have not told us what it is that you _want_ to happen, that is,
we do not know what "this" you are asking about.
You should use placeholders (or bind variables) in your queries
so that the DBI can handle proper quoting for you:
$sql = 'select * from hvw where lidnr = ?';
...
$sth->execute($record) or die "SQL Error: $DBI::errstr\n";
> Hints or directions on where to find this info are appreciated.
Read the documentation for the modules that you use:
perldoc DBI
contains this code:
$sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");
$sth->execute( $baz );
while ( @row = $sth->fetchrow_array ) {
print "@row\n";
}
Which is probably what you meant to ask for.
--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas