Bob Hunter wrote:

> # [1]: $sth->nfields                                  
>                          
> # [2]: $sth->{NUM_OF_FIELDS} 
> 
> 
> This is work in progress, as I keep going.
> 
> The last string replacement triggers an error, because
> $sth->{NUM_OF_FIELDS} is actually a string, while
> $sth->nfields is a number. Using the above in loops
> returns the following error:
> 
> Can't use string ("1496") as a HASH ref while "strict
> refs" in use at
>         <.....> line <...> (#2)
>     (F) Only hard references are allowed by "strict
> refs".  Symbolic
>     references are disallowed.  See perlref.
> 
> This is odd, however. In the manual "Programming the
> Perl DBI" I see this example:
> 
> for ( $i = 1 ; $i <= $sth->{NUM_OF_FIELDS} ; $i++ ) {
> print "Column $i is called $sth->{NAME}->[$i-1]\n";
> }
> 
> In my code, in the above line error, I have 
> 
>   for ($i=0; $i < $result->{NUM_OF_FIELDS} ; $i++)
> 
> which is hardly controversial. So, why am I having
> that error at all?

Because your $result is not a statement handle, according to the error message
above, it's just a string. A statement handle is obtained by a a call to
prepare(), like this:
  $sth=$dbh->prepare("some SQL text");

then you need to execute the query:
  $sth->execute;

and then $sth->{NUM_OF_FIELDS} will become available, assuming the execute has
been successful.

If I were to give an advice on beginning with DBI, I'd suggest using prepare()
and execute() everywhere, as opposed to easier variants like $dbh->do() or
$dbh->selectsomething() which will distract you from the real thing.
Then choose one method to pass query parameters (like bind_col or bind_param or
arguments to execute), and another one to retrieve SELECT resultsets (such as
$sth->fetchrow_array or $sth->fetchrow_hashref), and stick with these.

-- 
 Daniel
 PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org

Reply via email to