473,418 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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

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
23 24446
miller
1,089 Expert 1GB
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
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
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
1,089 Expert 1GB
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
1,089 Expert 1GB
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
4,059 Expert 2GB
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
I already put it in the begining of my code.
May 29 '07 #8
The problem is exactly on this line code:

$sth->execute() or die $dbh->errstr;
May 29 '07 #9
KevinADC
4,059 Expert 2GB
Then I do not know why you are getting an Internal Server Error.
May 29 '07 #10
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
4,059 Expert 2GB
is this line correct?

$sth.execute;
May 29 '07 #12
KevinADC
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
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
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
4,059 Expert 2GB
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
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
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
4,059 Expert 2GB
Are you running mod_perl? If so see if anything here is helpful:

mod_perl_traps(3) - Linux man page
May 31 '07 #21
Somebody help me! please
May 31 '07 #22
miller
1,089 Expert 1GB
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
4,059 Expert 2GB
Somebody help me! please
Did you bother to read the page I linked you to?
May 31 '07 #24

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

Similar topics

1
by: Asyn lordasyn | last post by:
Ok... I know there is got to be something simple that I forgot, but I for the life of me can't find it. Please, for the sake of the wall I keep slamming my head into, any insight would be helpful. ...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
by: David N | last post by:
Hi All, What is a best way to handle an undefined value object class that returned from a function. I have a function that call the ADO.NET ExecuteScalar() function and returns the object to...
6
by: TJS | last post by:
in vbscript there was a command called "execute" which would process a dynamic string, vb.net dropped that feature ... does anybody have a working solution for sale or free that will execute a...
17
by: Chen Shusheng | last post by:
Hi all, In fact, I want to let my memory run out. And see what will happen. My system is windowsXp. Memory is 256M.I think my cdes will apply more memory than I have. Codes are below: ...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
2
by: gmccammon | last post by:
I am getting an error that says that I can't call method "prepare" on an undefined value! Here is what I have as code.... I don't understand what is not defined. # opens connection to Access...
3
by: risrikanth | last post by:
hi, When iam executing the perl code iam getting error like"can't call method"bind_param_inout" with out package or object reference .here iam send little bit code plz check and can you tell me...
11
by: gautamga | last post by:
Hi All i have created the script which calls method from other script lib and while executing i get and error Can't call method "prepare" on an undefined value at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.