patricia_e has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I looked through 'Categorized Questions and Answers' and also searched this site and others, and I haven't come up with an answer.

I am trying to pluck email addresses from a mysql db to send reminder letters.

My code:
#!/usr/bin/perl -w use strict; use CGI; use DBI; # Declare and initialize variables my $host = 'hostname'; my $db = 'dbname'; my $db_user = 'dbuser'; my $db_password = 'mypassword'; my $dbh = DBI->connect("dbi:mysql:$db:$host", "$db_user", "$db_passwor +d" or die "Couldn't connect: $DBI::errstr"); my $sth = $dbh->prepare("SELECT * FROM feRegistrants" or die "Couldn't + prepare SQL: $DBI::errstr"); $sth->execute or die "Couldn't execute statement: " . $sth->errstr; my @data; while (@data = $sth->fetchrow_array()) { my $email = $data[5]; my $fname = $data[2]; print "Content-type:text/html\n\n"; open(MAIL, "|/usr/lib/sendmail -t -n") || die "Unable to open sendmail +"; print MAIL "To: $email \nFrom: the server\n"; print MAIL "Subject: Subject\n"; print MAIL<<EOF; Dear $fname,\n You have registered for *** workshop on the *** of ****, 2005. Please +let us know if you are unable to attend by emailing us at *** or call +ing us at ***. We look forward to working with you. EOF close(MAIL); } $dbh->disconnect();
The error messages I get are:
Useless use of string in void context at ./email2.cgi line 16.
Useless use of string in void context at ./email2.cgi line 16.
Can't connect(ch@ngeme), no database driver specified and DBI_DSN env var not set at ./email2.cgi line 16

Any suggestions?

Replies are listed 'Best First'.
Re: no database driver specified and DBI_DSN env var not set
by ambrus (Abbot) on Jan 03, 2005 at 22:05 UTC

    Putting the die outside the connect method call might help.

Re: no database driver specified and DBI_DSN env var not set
by injunjoel (Priest) on Jan 03, 2005 at 22:06 UTC
    Greetings all,
    Off the top of my head your parenthesis look off on your connection and prepare.
    try this:
    my $dbh = DBI->connect("dbi:mysql:$db:$host", "$db_user", "$db_passwor +d") or die "Couldn't connect: $DBI::errstr"; my $sth = $dbh->prepare("SELECT * FROM feRegistrants") or die "Couldn' +t prepare SQL: $DBI::errstr";


    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: no database driver specified and DBI_DSN env var not set
by runrig (Abbot) on Jan 04, 2005 at 00:25 UTC
    Use RaiseError and you won't need all those 'or die's on the DBI calls (and as an added bonus you will catch any errors that occur during the fetch):
    my $dbh = DBI->connect( "dbi:mysql:$db:$host", "$db_user", "$db_password", { RaiseError => 1 } );