Forum Moderators: coopster & phranque

Message Too Old, No Replies

Why doesn't Perl connect to MySql database?

         

webguru

9:47 am on Apr 28, 2003 (gmt 0)

10+ Year Member



I have two files: one is the config file; the other one is a simple perl script.

====================================================
config.dat:

$db_name = "db_xxxxx";
$uname = "u_xxxx";
$pass = "p_xxxx";
$dsn = "DBI:mysql:host=localhost;database=$dbname";

====================================================
foo.cgi:

#!/usr/local/bin/perl

use DBI;
use strict;

require "./config.dat";
my $dbh = &db_init();

my $sth = $dbh->prepare("select fname, lname from teams");
$sth->execute();

while (my @name = $sth->fetchrow_array() ) {
print "$name[0] $name[1]";
}

$sth->finish();
$dbh->disconnect();

sub db_init {
my ($dsn, $username, $password);

return ( DBI->connect($dsn, $uname, $pass, {PrintError => 0, RaiseError => 1} ));
}

====================================================

This is the error I got when running foo.cgi

Can't connect( HASH(0x817e734)), no database driver specified and DBI_DSN env var not set at ./foo.cgi line 22.

Please help.

Josk

10:02 am on Apr 28, 2003 (gmt 0)

10+ Year Member



You call db_init without any arguments... Perhaps you should supply some?

my $dbh = &db_init();

sub db_init {
my ($dsn, $username, $password);

return ( DBI->connect($dsn, $uname, $pass, {PrintError => 0, RaiseError => 1} ));
}