Can't call method "execute" on an undefined value at

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ticfranca
    New Member
    • May 2007
    • 14

    Can't call method "execute" on an undefined value at

    Hi, I'm getting this error in the code below:

    [CODE=perl]
    sub Pega_recorde {
    $database = 'bundinha';
    $host = 'localhost';
    $usuario = 'myhumoradm';
    $senha = 'my8xr2d2';

    $dbh=DBI->connect("DBI:m ysql:database=$ database;host=$ host","$usuario ","$senha") or die "Can't open DB: $!";

    $query="SELECT * FROM bundinha_rank";
    $sth=$dbh->prepare($query ) or die $dbh->errstr if $dbh->err;
    $rv=$sth->execute();

    While (@row = $sth->fetchrow_array ()) {
    $pontos= $row[6];
    }
    $rv=$dbh->disconnect;
    }
    [/CODE]

    The problem is in the line code : $rv=$sth->execute();
    I'll already verify and the database connection is OK. Please help me...I already tried almost everything....p lease help me

    Thiago França da Silva (São Paulo - Brazil) email: <removed by moderator>
    Last edited by miller; May 28 '07, 06:49 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Greetings Thiago,

    Your problem lies with this line:

    [CODE=perl]
    $sth=$dbh->prepare($query ) or die $dbh->errstr if $dbh->err;
    [/CODE]

    I understand that you are trying to do proper error checking, but the above line will only get executed "IF" $dbh->err exists.

    To do error checking for statement handles, I always include the "or die" statement only with the execute. So the following will fix your code:

    [CODE=perl]
    my $sth = $dbh->prepare($query );
    $sth->execute() or die $dbh->errstr;
    [/CODE]

    However, I would definitely advise you to clean up your code even more. None of your variables are currently defined with "my", so I can probably assume that you are not including "use strict;" at the beginning if your script . This is bad.

    Cleaned up a little, your script would look something like this:

    [CODE=perl]
    sub Pega_recorde {
    my $database = 'bundinha';
    my $host = 'localhost';
    my $usuario = 'myhumoradm';
    my $senha = 'my8xr2d2';

    my $dbh = DBI->connect("DBI:m ysql:database=$ database;host=$ host","$usuario ","$senha") or die "Can't open DB: $!";

    my $sth = $dbh->prepare(q{SELE CT * FROM bundinha_rank}) ;
    $sth->execute() or die $dbh->errstr;

    while (my @record = $sth->fetchrow_array ()) {
    $pontos = $record[6];
    }

    $sth->finish;
    $dbh->disconnect;
    }
    [/CODE]

    - Miller

    Comment

    • ticfranca
      New Member
      • May 2007
      • 14

      #3
      Hi, Miller

      I updated my code , but now i'm getting an error at the same line :

      Internal Server Error
      The server encountered an internal error or misconfiguratio n and was unable to complete your request.
      Please contact the server administrator

      Do you have any suggestion....?


      sub Pega_recorde {
      my $database = 'bundinha';
      my $host = 'localhost';
      my $usuario = 'myhumoradm';
      my $senha = 'my8xr2d2';

      my $dbh = DBI->connect("DBI:m ysql:database=$ database;host=$ host","$usuario ","$senha") or die "Can't open DB: $!";

      my $sth = $dbh->prepare(q{SELE CT * FROM bundinha_rank}) ;
      $sth->execute() or die $dbh->errstr;

      while (my @record = $sth->fetchrow_array ()) {
      $pontos = $record[6];
      }

      $sth->finish;
      $dbh->disconnect;
      }


      Thanks, Thiago

      Comment

      • ticfranca
        New Member
        • May 2007
        • 14

        #4
        Internal server error

        i'm getting an error at the line : $sth->execute() or die $dbh->errstr;

        the error is : The server encountered an internal error or misconfiguratio n and was unable to complete your request.
        Please contact the server administrator


        I already check permission of cgi-bin folder and the permission of this script file.
        Everything is OK. Somebody help me?Please..

        Internal Server Error
        The server encountered an internal error or misconfiguratio n and was unable to complete your request.
        Please contact the server administrator

        Do you have any suggestion....?


        sub Pega_recorde {
        my $database = 'bundinha';
        my $host = 'localhost';
        my $usuario = 'myhumoradm';
        my $senha = 'my8xr2d2';

        my $dbh = DBI->connect("DBI:m ysql:database=$ database;host=$ host","$usuario ","$senha") or die "Can't open DB: $!";

        my $sth = $dbh->prepare(q{SELE CT * FROM bundinha_rank}) ;
        $sth->execute() or die $dbh->errstr;

        while (my @record = $sth->fetchrow_array ()) {
        $pontos = $record[6];
        }

        $sth->finish;
        $dbh->disconnect;
        }


        Thanks, Thiago (São Paulo Brazil)

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          Hello Thiago,

          Please do not double post your questions. This is covered in the posting guidelines.

          I've merged the threads.

          - MODERATOR

          Comment

          • miller
            Recognized Expert Top Contributor
            • Oct 2006
            • 1086

            #6
            Hello Thiago,

            I have no explanation for why you would be getting an Internal Server Error. There is nothing in the changes that I proposed that would have introduced such an error, so it must be something in the rest of your code.

            Regards,
            - Miller

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              add this line at the beginning of your script, after the shebang line:

              use CGI::Carp qw/fatalsToBrowser/;

              and retry your script.

              Comment

              • ticfranca
                New Member
                • May 2007
                • 14

                #8
                I already put it in the begining of my code.

                Comment

                • ticfranca
                  New Member
                  • May 2007
                  • 14

                  #9
                  The problem is exactly on this line code:

                  $sth->execute() or die $dbh->errstr;

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Then I do not know why you are getting an Internal Server Error.

                    Comment

                    • ticfranca
                      New Member
                      • May 2007
                      • 14

                      #11
                      Hi, I did some changes in my code and now it return me this HASH -> DBI::st=HASH(0x 8272054), however , it does'nt return nothing of the database. Can somebody help me...i need resolve that as soon as possible... my job depends on that. Please....


                      sub Pega_recorde {
                      my $database = 'bundinha';
                      my $host = 'localhost';
                      my $usuario = 'myhumoradm';
                      my $senha = 'my8xr2d2';

                      my $dbh = DBI->connect("DBI:m ysql:database=$ database;host=$ host","$usuario ","$senha") or die "Can't open DB: $!";

                      my $sth = $dbh->prepare(q{SELE CT * FROM bundinha_rank}) ;
                      $sth.execute;

                      while (my @row = $sth->fetchrow_array ()) {
                      $pontos = $row[6];
                      }

                      $sth->finish;
                      $dbh->disconnect;
                      }

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        is this line correct?

                        $sth.execute;

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          try changing the semi-colon after $database ';' to a colon ':'

                          my $dbh = DBI->connect("DBI:m ysql:database=$ database:host=$host","$u suario","$senha ")

                          Comment

                          • ticfranca
                            New Member
                            • May 2007
                            • 14

                            #14
                            Originally posted by KevinADC
                            try changing the semi-colon after $database ';' to a colon ':'

                            my $dbh = DBI->connect("DBI:m ysql:database=$ database:host=$host","$u suario","$senha ")
                            Hi Kevin,

                            When the line was like that :

                            $sth->execute() or die $dbh->errstr;


                            The program returned me INTERNAL SERVER ERROR...only when i change for

                            $sth.execute;

                            The error disappeared and $sth returned something. I don't know what more i can do!.I tried almost everything....i f you can, help me...Thanks


                            Thiago França (São Paulo - Brazil)

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              Originally posted by ticfranca
                              Hi Kevin,

                              The error disappeared and $sth returned something. I don't know what more i can do!.I tried almost everything....i f you can, help me...Thanks


                              Thiago França (São Paulo - Brazil)
                              Make the change I suggested above (change the semi-coln ';' to a colon ':') and try your script again.

                              But also change this:

                              $sth.execute;

                              back to:

                              $sth->execute() or die $dbh->errstr;

                              $sth.execute is not going to do anything.

                              Comment

                              Working...