Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Perhaps a wise monk can enlighten me but this MySQL error is boggling my mind. How can I get an undefined value on bind_columns() if my variables are set prior to the bind? I never had this error before.
Can't call method "bind_columns" on an undefined value at manage.pl li +ne 73.
My code (with line numbers) is here
000068: my $data = qq(SELECT catname, path, root FROM categories WH +ERE root = "1" ORDER BY catname ASC); 000069: my $sth = $dbh->prepare($data); 000070: $sth->execute() or die $dbh->errstr; 000071: 000072: my ($catname, $path, $root); 000073: my $sth->bind_columns(\$catname, \$path, \$root); 000074: 000075: while($sth->fetch) 000076: { 000077: print qq~ 000078: <a href="manage.pl?process=categories&path=$path">$catna +me</a><br> 000079: ~; 000080: }
Can anyone see what might be going wrong?

2007-03-29 Retitled by planetscape, as per Monastery guidelines
Original title: 'an't call method "bind_columns" on an undefined value'

Replies are listed 'Best First'.
Re: Can't call method "bind_columns" on an undefined value
by betterworld (Curate) on Mar 28, 2007 at 00:33 UTC
    The return value of "my" is always undefined, as "my" makes new variables. You should try leaving out the "my" in line 73, because you don't want a new variable, you want the old one.
Re: Can't call method "bind_columns" on an undefined value
by grinder (Bishop) on Mar 28, 2007 at 08:00 UTC

    I should point out that:

    my $sth = $dbh->prepare($data);

    may fail if the SQL statement is syntactically incorrect. That's not the case here, since $sth->execute() succeeds afterwards, but it's a good idea to get into the habit of checking the return from prepare as well.

    Disclaimer: for all I know, prepare might be a no-op for MySQL, but that's certainly not the case for other database engines. Get into the habit of doing it anyway, and you'll have less strife when you work with other dbs.

    • another intruder with the mooring in the heart of the Perl

Re: Can't call method "bind_columns" on an undefined value
by andreas1234567 (Vicar) on Mar 28, 2007 at 08:39 UTC
    Remove "my" on line 000073 and you'll be fine:
    $ diff bind_columns.pl.1 bind_columns.pl 52c52 < my $sth->bind_columns(\$name); --- > $sth->bind_columns(\$name); $ perl bind_columns.pl.1 "my" variable $sth masks earlier declaration in same scope at bind_col +umns.pl.1 line 52. Can't call method "bind_columns" on an undefined value at bind_columns +.pl.1 line 52. $ perl bind_columns.pl foo bar
    You should also probably use warnings which will give you the additional warning "variable $sth masks earlier declaration in same scope" on the same line.

    --
    Andreas
A reply falls below the community's threshold of quality. You may see it by logging in.