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
232 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now