Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

SCelia

Programmer
Feb 27, 2002
82
CA
That error is beginning to get on my nerves. I just can't figure out what I messed up. Hoping you folks will be able to tell me.

statement:

$table = 'tourney';

my $sth = $dbh->prepare("INSERT INTO $table VALUES (?,?,?,?,?,?)") or die "Couldn't prepare statement: " . $dbh->errstr;

execute:

$sth->execute($rank, $name, $land, $nw, $game, $set) or die "Couldn't execute statement: " . $sth->errstr;

Niether one dies with errors but when it tries to execute the statement the program stops execution and I see "Can't call method "execute" on an undefined value at..." in my error log.

Any ideas?

Thanks,
Celia
 
That error means that $dbh is undefined. Are you sure that
Code:
$dbh = DBI->connect(...)
is succeeding?
 
My connect code:

use DBI;
my $dbh = DBI->connect('DBI:mysql:******', '*****', '*****') or die "Couldn't connect to database: " . DBI->errstr;

It doesn't die or return any errors so I don't see what's going wrong here... In fact it was working fine just recently before I started modifying it. The database stuff is mostly unaltered tho, really befuddles me.

What do you make of it? Celia
 
I really have no idea. Try doing this just to see if $sth is actually undefined. Try this:

Code:
my $sth = $dbh->prepare("INSERT INTO $table VALUES (?,?,?,?,?,?)") or die "Couldn't prepare statement: " . $dbh->errstr;
print "\$sth is $sth";

For $sth, you should get something like DBI=HASH(x090707). If you get nothing, it probably is undefined for some reason. If you do get a memory location, I'm clueless.

 
$sth is DBI::st=HASH(0x83b6204)

So if sth is defined what on earth could the problem be? Almost makes me want to send the data to a php script and get it to do that database handling.

clueless also, Celia
 
Hi.

The problem is that You realy do not have connection to DB. As it was mentioned above dbh is not defined. For example maybe u use global connection, and yuor DB accepts only localhost.
 
No, that can't be the problem because I removed my queries and added some that I know worked. They worked. So I'm definately connecting fine to the database. I'm guessing the problem must be my querries but I can't see what's wrong with them.

The vars = 1 , Bavaria (#2) , 7341 , 10241507 , A, May 2002

The query:

$table = 'tourney';

my $sth = $dbh->prepare("INSERT INTO $table VALUES (?,?,?,?,?,?)") or die "Couldn't prepare statement: " . $dbh->errstr;

execute:

$sth->execute($rank, $name, $land, $nw, $game, $set) or die "Couldn't execute statement: " . $sth->errstr;


The table structure:

rank [smallint(2)] name [varchar(30)] land [mediumint(7)] nw [int(11)] game [char(1)] set [varchar(12)]



I know that some of the items I insert later using that statement in a loop are messed up:

The vars = Bavaria (#2) , 7341 , 10241507 , 2, A, May 2002

But would that cause the problem? It never loops through because it fails when it first hits the execute query.


Celia
 
Try taking the $sth out of the equation:
Use the DBI method do():
Code:
# after defining $dbh with a connect():
$it_worked = $dbh->do("INSERT ... yada yada");

print "GOOD" if $it_worked;
print "No Good" unless $it_worked;
See if that works...

--jim
 
it doesn't work at all... as in 500 error... how do I use do?

my code...

$it_worked = $dbh->do("INSERT INTO $table VALUES ($rank, $name, $land, $nw, $game, $set)");

print "GOOD" if $it_worked;
print "No Good" unless $it_worked;

Thanks, Celia
 
Celia,

After your 'do' statement above, what is the value of $DBI::errstr ? Mike
________________________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
There is no value of the $DBI::errstr. The statement didn't put anything into the database table but it also didn't generate any errors. Just like the $sth stuff I was doing. Any ideas what it might be? Celia
 
I also tried $dbh->errstr. No luck. Celia
 
In my code:

$it_worked = $dbh->do("INSERT INTO $table VALUES ($rank, $name, $land, $nw, $game, $set)") or die "Couldn't do statement: " . $dbh->errstr; #also tried DBI->errstr

print "GOOD" if $it_worked;
print "No Good" unless $it_worked;

The print functions never execute. The code dies at the query with or without the die function. No errors probably for the reason that it dies as mentioned. You can see my queries and my table structure. Even the data in the queries. We know the database connect is successful so what could be going wrong. I just can't figure it out.

Baffled, Celia
 
Ok.... Pretty annoying...

Your connect statement, are you setting the RaiseError parameter? Like this:

$dbh = DBI->connect($dsn, $user, $password,
{ RaiseError => 1, AutoCommit => 0 });
Mike
________________________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
DO you have this at the very top of your script:

use CGI::Carp qw/fatalsToBrowser/;


???? This will help error messages from perl make it to the browser...

--jim
 
Do you have warnings enabled
#!/usr/bin/perl -w

and are you using strict?

use strict;

This might illuminate a subtle error in your code.

jaa
 
I don't really understand what strict is/does. Do I have to declare all my variables using "my" when using strict? Celia
 
strict forces you to learn/use good programming habits until you really know what you are doing. Yes, you have to declare all your variables with my or our. This prevents you from inadvertanly using a varible that has the same name as yours that is declared in another package that you didn't know about, which could have unexpected results.

It also prevents the use of soft references, such as;

$expected = "unexpected";
$state = "expected";
print $$state; #oops, i accidentally typed too many $'s

And it prohibits the calling of subroutines using "bare words", which may be nice for Perl poetry but is bad for cgi scripts.

sub expected {
return "unexpected";
}

my %pristinehash = ( state => expected );
#oops, forgot to put quotes around the value
print "$_ => $pristinehash{$_}\n" for (keys %pristinehash);

If your're not using strict, then you should expect the unexpected. :)

jaa

 
Thanks justice, some of thse posts deserve stars but I'm waiting until the problem is solved before I dish them out. I don't want people to a see a post with 3or 4 stars on it and assume the problem is fixed. Celia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top