?
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
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
xanius earned 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans
Suggested Courses

777 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