Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Can't call method "do" - DBI Troubles

by vbrtrmn (Pilgrim)
on Feb 03, 2003 at 13:59 UTC ( [id://232236]=perlquestion: print w/replies, xml ) Need Help??

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

Hey, I'm having some probs w/ DBI today. My code bombs with:

Can't call method "do" on an undefined value at /var/www/html/sites/www.auroraborealis.org/cgi-bin/gbdbconnect.cgi line 19.

Here's my code:

#!/usr/bin/perl -w use strict; use DBI; use CGI::Carp "fatalsToBrowser"; use CGI ":all"; my $username = 'USERNAME'; my $pass = 'PASSWORD'; my $db = 'DBI:mysql:auroraborealis:localhost'; my $db_object = DBI->connect($db, $username, $pass); print header(); print "<PRE>"; my $sql = "SELECT * FROM guestbook ORDER BY name"; my $dbh->do($sql); $dbh->disconnect(); exit;

--
paul

Replies are listed 'Best First'.
Re: Can't call method "do" - DBI Troubles
by broquaint (Abbot) on Feb 03, 2003 at 14:05 UTC
    Change your database assignment line to
    my $dbh = DBI->connect($db, $username, $pass);
    And the line with do on it to
    $dbh->do($sql);
    You'll also probably want to check the return of both of those statements in case they fail.
    HTH

    _________
    broquaint

Re: Can't call method "do" - DBI Troubles
by robartes (Priest) on Feb 03, 2003 at 14:17 UTC
    To expand on broquaint's perfectly correct answer:

    The reason your code is not working is because you first assign the reference to the DBI object (commonly called the Database handle, hence $dbh) to db_object:

    my $db_object = DBI->connect($db, $username, $pass);
    after which you try to access it through a new variable $dbh, which is of course undefined:
    my $dbh->do($sql);
    hence the error. I suspect that you put in the my because the compiler complained about a missing package name on $dbh. You need to drop the my.

    CU
    Robartes-

Re: Can't call method "do" - DBI Troubles
by Cabrion (Friar) on Feb 03, 2003 at 14:28 UTC
    Adding one more suggestion to this list of correct answers, you should probably trap some errors with . . .
    my $dbh = DBI->connect($db, $username, $pass) || die $DBI->errstr();
    and
    my $dbh->do($sql) || die $dbh->errstr();
    to catch any underlying problems with the connection process or underlying SQL execution.

      You can use the RaiseError option in the connect() method to automatically "status check" all calls to database functions, a nice little time saver. Note: you still have to status check the connect() method itself. . .

      my $dbh=DBI->connect($db,$username,$pass,{RaiseError => 1}) or die("Co +uldn't connect to database ".DBI::errstr);
      and the statement
      $dbh->do($sql);
      will automatically have status checking done for it, as well as any other calls to DBI.

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

Re: Can't call method "do" - DBI Troubles
by electrosphere (Beadle) on Feb 03, 2003 at 17:14 UTC
    Perl is asking you to look hard at line 19...

    You must run the 'do' on a defined database handle. The database handle is ruturned by the DBI->connect method. In your code this handle is stored in $db_object (I think $dbh would be a better identifier - database Handle).

    Change line 19 to: $db_object->do($sql);

    I think you want to use the prepare/execute methods when working with SQL Selects as the do method does not return a statement handle (see perldoc DBI for more info)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://232236]
Approved by AcidHawk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-24 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found