Solved

MYSQL database connection script returns error 'Program fragment delivered error ``Can't call method "prepare" on unblessed reference at /var/www/html/email/cgi-bin/index.pl line 43.'

Posted on 2006-06-11
4
246 Views
Last Modified: 2006-11-18
This on is driving me crazy. Iam at my last rope.

index.pl

use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);
use Text::Template;
use DBI;
use D1;
use DF;


sub get_all_emails
{
my $DBH = D1->get_db_connection;
my ($ary_ref);
my ($email);
my ($buf) = '';
my $sql = "SELECT email from emails";

my $sth = $DBH->prepare($sql);

$sth->execute();

while($ary_ref = $sth->fetch()) {      
                        ($email, $id) = @{$ary_ref};
                        
                        $buf .= sprintf($DF::email_list_all_fmt, $email, $id, $id);
  }
  return $buf;
}

D1.pm (in the same directory)
package D1;

use DBI;
#use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);


BEGIN {

}

#====END BEGIN====#
sub get_db_connection
{
my %attr = (
      PrintError => 0,
      RaiseError => 0
);

my $DBH = DBI->connect("DBI:mysql:localhost", "user1010", "p10s45gf", \%attr);
$DBH->{PrintError} = 1;
return $DBH;
}

#================================================================================================
# logical sub-routines

END {
}

return 1;


DF.pm is just formatting instructions with a bunch of escaped html code and %s inserted into the right places. It is also in the same directory.

Thanks a million in advance,guys!
~Lil <not on the answering end this time - arrrrgh!>
0
Comment
Question by:Lil
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
xanius earned 250 total points
ID: 16883500
Lil,

I think the connect in get_db_connection is not working, returning undef. Therefore $DBH is undef and not a DBI-Blessed object, hence the error. You should generally handle errors when communicating with processes outside perl (mysql in this case). either set the PrintError and/or RaiseError to 1 or check the DBI-errors after the database connection.

The following modification of get_db_connection shoudl give you the first clues:

sub get_db_connection
{
my %attr = (
     PrintError => 1,
     RaiseError => 1
);

my $DBH = DBI->connect("DBI:mysql:localhost", "user1010", "p10s45gf", \%attr);
# $DBH->{PrintError} = 1;
return $DBH;
}

# Cheers
# Xanius

0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16883507
You are calling  as if it was a method in the D1 package, but it is never blessed with that class
my $DBH = D1->get_db_connection;

I think what you want is a simple non object based call to the subroutine
my $DBH = D1::get_db_connection;

0
 
LVL 3

Author Comment

by:Lil
ID: 16973560
Xanius,

I have changed the connection code the way you've described and - it works! Now finally (after a few hard days and lots of coffee) it adds, deletes, selects, does evrything it is suppsoed to. Thanks again and please accept the offered points with my thanks! If you have a few sec and just can shoot off a description of why this worked, I'd appreciated it, but of course this is not necessary.

Teralplane, I've tried it both ways but until I've changed the connection as described, nothing worked.

Thanks again all,
~Lil
0
 
LVL 10

Expert Comment

by:xanius
ID: 16979591
~Lil,

I'm happy ty get the points, but I don't quie understand how my suggestion made your program work.
my suggestion is only altering the error handling within DBI, it is not changing anything else. The only reason I can think of is that your mysql server might have had a problem, or was down as you were asking the question.

If this code is part of a some web site, when everything is working you should set both PrintError and RaiseError back to 0 but check on $DBH an subsequently, if $DBH is undef catch the DBI error and produce a reasonable error message for the user lik "Sorry, the database is currently not available" or somerthing.

Cheers
Xanius
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 181
perl search and replace 6 172
Perl Awk Need Help 3 138
Perl passing in variables to do substitution 6 79
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question