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

I am trying to call a simple line from a table with only two lines. This whole shippet seems to work until the bind line, where I see the error:
Can't call method "bind_columns" without a package or object refer +ence at /var/www/cgi-bin/noctracker.pl line 95.
The code snippet in question is:
use DBI; $dbh = DBI->connect("DBI:mysql:gds:localhost", "tools", "meep") or + die ("No love for the DBI connect"); $sql = qq{ SELECT uname, fname, fgroup, dmod, accts FROM noctracke +r WHERE uname = 'gwalker'} ; $sth = $dbh->do( $sql ) or die ("Could not select"); my( $uname, $fname, $fgroup, $dmod, $accts ); $sth->bind_columns( undef, \$uname, \$fname, \$fgroup, \$dmod, \$a +ccts ); # this is line 95

Replies are listed 'Best First'.
Re: DBI and the hell it has made my life...
by eric256 (Parson) on Aug 21, 2003 at 20:48 UTC

    You are trying to assign $sth as a statement handle, but you can only do that when you prepare the statment first. A prepare call returns a statment handle,execute, then you bind your columns.

    Here is the code form bind_columns on the DBI page of CPAN

    $dbh->{RaiseError} = 1; # do this, or check every call for errors $sth = $dbh->prepare(q{ SELECT region, sales FROM sales_by_region }) +; $sth->execute; my ($region, $sales); # Bind Perl variables to columns: $rv = $sth->bind_columns(\$region, \$sales); while ($sth->fetch) { print "$region: $sales\n"; }

    Notice how they first prepare,then execute,then bind, then fetch. Good luck!

    Updated: I confused bind_params and bind_colunms. thanks gmax for pointing out my mistake.

    ___________
    Eric Hodges
Re: DBI and the hell it has made my life...
by princepawn (Parson) on Aug 21, 2003 at 20:56 UTC
    Your first issue is that you did not use strict. Second, connect with RaiseError set to 1 so you can get rid of the or die in your code. Third, do returns the number of rows affected or undef, you need to use execute as the docs state:
    It should not be used for SELECT statements because it does not return a statement handle (so you can't fetch any data).

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality... download and use The Emacs Code Browser

Re: DBI and the hell it has made my life...
by lestrrat (Deacon) on Aug 21, 2003 at 20:49 UTC

    Yes, the error is expected, and the behavior is correct. do() doesn't return a Statement Handle. Use prepare() instead

Re: DBI and the hell it has made my life...
by coyotlgw (Initiate) on Aug 21, 2003 at 21:08 UTC
    Behold, the power of "RTFM"!

    Sorry, while you three were responding, I re-read the PerlDBI book's section on this and discovered that I could ont use do... had to use prepare and execute.

    Sorry about that...