By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

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

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

Expand|Select|Wrap|Line Numbers
  1. sub Pega_recorde {
  2.     $database = 'bundinha';
  3.     $host = 'localhost';
  4.     $usuario = 'myhumoradm';
  5.     $senha = 'my8xr2d2';
  6.  
  7.     $dbh=DBI->connect("DBI:mysql:database=$database;host=$host","$usuario","$senha") or die "Can't open DB: $!";
  8.  
  9.     $query="SELECT * FROM bundinha_rank";
  10.     $sth=$dbh->prepare($query) or die $dbh->errstr if $dbh->err;
  11.     $rv=$sth->execute();
  12.  
  13.     While (@row = $sth->fetchrow_array()) { 
  14.         $pontos= $row[6];
  15.     }
  16.     $rv=$dbh->disconnect;
  17. }
  18.  
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....please help me

Thiago França da Silva (São Paulo - Brazil) email: <removed by moderator>
May 28 '07 #1
Share this Question
Share on Google+
23 Replies


miller
Expert 100+
P: 1,089
Greetings Thiago,

Your problem lies with this line:

Expand|Select|Wrap|Line Numbers
  1.     $sth=$dbh->prepare($query) or die $dbh->errstr if $dbh->err;
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. my $sth = $dbh->prepare($query);
  2. $sth->execute() or die $dbh->errstr;
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. sub Pega_recorde {
  2.     my $database = 'bundinha';
  3.     my $host = 'localhost';
  4.     my $usuario = 'myhumoradm';
  5.     my $senha = 'my8xr2d2';
  6.  
  7.     my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host","$usuario","$senha") or die "Can't open DB: $!";
  8.  
  9.     my $sth = $dbh->prepare(q{SELECT * FROM bundinha_rank});
  10.     $sth->execute() or die $dbh->errstr;
  11.  
  12.     while (my @record = $sth->fetchrow_array()) { 
  13.         $pontos = $record[6];
  14.     }
  15.  
  16.     $sth->finish;
  17.     $dbh->disconnect;
  18. }
  19.  
- Miller
May 28 '07 #2

P: 14
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 misconfiguration 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:mysql:database=$database;host=$host" ,"$usuario","$senha") or die "Can't open DB: $!";

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

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

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


Thanks, Thiago
May 28 '07 #3

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

the error is : The server encountered an internal error or misconfiguration 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 misconfiguration 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:mysql:database=$database;host=$host" ,"$usuario","$senha") or die "Can't open DB: $!";

my $sth = $dbh->prepare(q{SELECT * 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)
May 28 '07 #4

miller
Expert 100+
P: 1,089
Hello Thiago,

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

I've merged the threads.

- MODERATOR
May 28 '07 #5

miller
Expert 100+
P: 1,089
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
May 28 '07 #6

KevinADC
Expert 2.5K+
P: 4,059
add this line at the beginning of your script, after the shebang line:

use CGI::Carp qw/fatalsToBrowser/;

and retry your script.
May 29 '07 #7

P: 14
I already put it in the begining of my code.
May 29 '07 #8

P: 14
The problem is exactly on this line code:

$sth->execute() or die $dbh->errstr;
May 29 '07 #9

KevinADC
Expert 2.5K+
P: 4,059
Then I do not know why you are getting an Internal Server Error.
May 29 '07 #10

P: 14
Hi, I did some changes in my code and now it return me this HASH -> DBI::st=HASH(0x8272054), 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:mysql:database=$database;host=$host" ,"$usuario","$senha") or die "Can't open DB: $!";

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

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

$sth->finish;
$dbh->disconnect;
}
May 29 '07 #11

KevinADC
Expert 2.5K+
P: 4,059
is this line correct?

$sth.execute;
May 29 '07 #12

KevinADC
Expert 2.5K+
P: 4,059
try changing the semi-colon after $database ';' to a colon ':'

my $dbh = DBI->connect("DBI:mysql:database=$database:host=$host","$usuario","$senha")
May 29 '07 #13

P: 14
try changing the semi-colon after $database ';' to a colon ':'

my $dbh = DBI->connect("DBI:mysql:database=$database:host=$host","$usuario","$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....if you can, help me...Thanks


Thiago França (São Paulo - Brazil)
May 29 '07 #14

KevinADC
Expert 2.5K+
P: 4,059
Hi Kevin,

The error disappeared and $sth returned something. I don't know what more i can do!.I tried almost everything....if 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.
May 29 '07 #15

P: 14
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.
I replaced semi-colons and then the connection failed.
I return to $sth->execute() or die $dbh->errstr;, then i get internal server error again.

I'm desperated....I try ...try ...try and the script doesn't works.

Thanks Kevin
May 30 '07 #16

P: 14
Merged Thread. Title: Error "Internal Server Error" on the execute command line

I have a script and in the line

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

i get an error INTERNAL SERVER ERROR

I saw many tutorials and already checked the permissions in 755, Perl path , possibles wrong @ and $ on the script and everything is right.


below my complete sub



sub Pega_recorde {
my $database = 'bundinha';
my $host = 'localhost';
my $usuario = 'morad';
my $senha = 'm8x2';

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

my $sth = $dbh->prepare(q{SELECT pontos FROM bundinha_rank});
$sth->execute or die $dbh->errstr;
while (@record = $sth->fetchrow_array()) {
$pontos = $record[6];
}
$sth->finish;
$dbh->disconnect;
}

When I take off the line in bold from script, it works fine. What's wrong in my execute() command?


Somebody help me

Thiago
May 30 '07 #17

KevinADC
Expert 2.5K+
P: 4,059
Look in the server error log if possible. At this point I do not know why you are getting an internal server error.
May 30 '07 #18

P: 14
Merged Thread Title: Error perl recolocation

I tried to execute my cgi file in linux by command line for check error because by browser i had the message internal server error.

I get an error like that:

perl: relocation error: /usr/lib/perl5/site_perl/5.6.1/i386-linux/auto/DBD/mysql
/mysql.so: undefined symbol: mysql_warning_count

What would be this error...can somebody help me?

thanks.

Thiago
May 30 '07 #19

P: 14
I executed it by linux command line and it returns me an error like that:


perl: relocation error: /usr/lib/perl5/site_perl/5.6.1/i386-linux/auto/DBD/mysql/mysql.so: undefined symbol: mysql_warning_count

Did you know something about this error?
May 31 '07 #20

KevinADC
Expert 2.5K+
P: 4,059
Are you running mod_perl? If so see if anything here is helpful:

mod_perl_traps(3) - Linux man page
May 31 '07 #21

P: 14
Somebody help me! please
May 31 '07 #22

miller
Expert 100+
P: 1,089
Hello ticfranca,

It sounds very much like you are new to both DBI and MySQL and maybe even perl. I would suggest that you start a little smaller when trying to get this setup properly at the admin level before moving on to cgi and apache integration.

Take this little script for example. You simply run it from the command prompt and it connects to MySQL and prints out the tables in the database that you specify. If you can't get this to work, then hopefully you can track down the problem to either something with the configuration of your mysql server, or maybe your connect string information.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2.  
  3. use DBI;
  4.  
  5. use strict;
  6.  
  7. # Database Connect Values
  8. my $name = 'bundinha';
  9. my $user = 'myhumoradm';
  10. my $pass = 'my8xr2d2';
  11. my $host = 'localhost';
  12.  
  13. # Database URL
  14. my $url = "dbi:mysql:$name:$host";
  15.  
  16. my $dbh = DBI->connect($url, $user, $pass) or die "Database connection failed.";
  17.  
  18. # Determine Existing Tables
  19. my $name;
  20. my $sth = $dbh->prepare(q{SHOW TABLES});
  21. $sth->execute or die $dbh->errstr;
  22. $sth->bind_columns(\$name);
  23. while ($sth->fetch) {
  24.     print "$name\n";
  25. }
  26. $sth->finish; undef $sth;
  27.  
  28. $dbh->disconnect; undef $dbh;
  29.  
  30. 1;
  31.  
  32. __END__
  33.  
When I run this on my local machine, I get the following results:

Expand|Select|Wrap|Line Numbers
  1. > perl scratch.pl
  2. DBI connect('bundinha:localhost','myhumoradm',...) failed: Can't connect to MySQL server on 'localhost' (10061) at scratch.pl line 16
  3. Database connection failed. at scratch.pl line 16.
  4.  
This is good, because I don't actually have mysql installed on my home computer so it's no surprise that it can't connect to MySQL server.

On my development machine I get the following results
Expand|Select|Wrap|Line Numbers
  1. $ perl scratch.pl 
  2. DBI->connect(bundinha:localhost) failed: Access denied for user: 'myhumoradm@localhost' (Using password: YES) at scratch.pl line 16
  3. Database connection failed. at scratch.pl line 16.
  4.  
This is also as expected because while I have a MySQL server, I have no permissions setup for user myhumoradm.

Getting DBI to work is all about putting yourself in a position to receive meaningful error messages. Without those you can never come close to knowing how to fix something that goes wrong.

Your first error message was meaningful, although you didn't know what it meant. Can't call method "execute" on an undefined value at This meant quite simply that the object that you were trying to run execute on was undefined. This led me to immediately ask, what value or object is this error message talking about? Well, it could only be one, $sth. Then I ask why is this value undefined? Well, the only way to know this is to look at the previous line and see that your assignment was malformed because of the inclusion of the if statement. You fixed that, great.

Next you came back with an "Internal Server Error". Well this means that your apache server died for some reason, but unfortunately you don't know the reason. Well, we can presume that because you only changed two lines that the die was in this line "$sth->execute() or die $dbh->errstr". However, to be able to diagnose this we must know what the errstr was. To get this you either need to look at the error log or run the cgi script outside of the apache environment, which you eventually decided to do.

This lead you to the next clue. " perl: relocation error: ... undefined symbol: mysql_warning_count". Guess what, I have no idea what this means, so the quickest place to be enlightened is google. This actually doesn't bring up too much information other than what this function is. "Description: Returns the number of warnings generated during execution of the previous SQL statement. ". This isn't very enlightening although it does tell us that DBI is at least trying to work.

So this brings me to my only suggested approach. Limit the number of possible causes by reducing your MySQL code to the minimum necessary to test if things are working. The script that I just provided should do that, and if this works, then see about testing the exact query that you use in your cgi script.

Then if that works see about importing it into a basic CGI script. And on and on, slowly adding layers of uncertainty and risk until you either reach your final goal or something breaks that you can recognize and diagnose.

This is called debugging. It's tedious. But it's something that all programmers and developers have to learn as we all make mistakes. We just must aim to setup our environments to minimize risk and be certain that errors are reported in meaningful ways.

Good luck,
- Miller
May 31 '07 #23

KevinADC
Expert 2.5K+
P: 4,059
Somebody help me! please
Did you bother to read the page I linked you to?
May 31 '07 #24

Post your reply

Sign in to post your reply or Sign up for a free account.