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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

864 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

20 Experts available now in Live!

Get 1:1 Help Now