# Solved: Perl CGI and DBI, MySQL, DBI->connect



## andynic (May 25, 2007)

Hi,
I am using Perl v. 5.10 and MySQL v.5.1.31 Community Server (GPL).

Given this 8 line segment of code from a Perl CGI script:
my $db = "gfr";
my $un = "gfr";
my $pw = "gfr";
my $dbH = "";
$dbH = DBI->connect("DBI:ODBC:$db",$un,$pw) ||
die "Got error when connecting to un=$un, pw=$pw, db=$db\n".DBI->errstr;
print "xxx";
exit;

It executes perfectly when I run it from a command window.
i.e. perl -w prgNm.cgi

When I call the script from an HTML file like this:

Load new image filenames

I get the error:
Got error when connecting to un=gfr, pw=gfr, db=gfr
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at C:/Documents/Documents_20090101_to_20091231/Computing/GalleryForRent_Maintenance/gfr-bin/prgNm.cgi line 49.

If I comment out the two lines:
# $dbH = DBI->connect("DBI:ODBC:$db",$un,$pw) ||
# die "Got error when connecting to un=$un, pw=$pw, db=$db\n".DBI->errstr;
I get the expected "xxx" in the web browser (so I know that it does not lie with the Apache Server's httpd.conf file).

Would anyone have any ideas?

Thanks,
Andynic
PS: To try to avoid using ODBC, I have tried to obtaiin a DBD-mysql module for Perl 5.10 and MySQL v5.1 but nothing I have found on the web works.


----------



## andynic (May 25, 2007)

Problem solved: The problem did, in fact, lie with the Apache httpd.conf file!

After following the instructions at 
http://www.apachelounge.com/viewtopic.php?p=12529
things worked as they should. The instructions given there are these:

GENERAL SETUP FOR MYSQL 5.0 ON WINDOWS (mod_dbd)

1. You must have a working MySQL 5.0 database and you must install the MySQL ODBC driver (which MySQL calls "MySQL Connector/ODBC"). The MySQL ODBC driver can be downloaded here: http://dev.mysql.com/downloads/connector/odbc/5.1.html
(Note: I changed the download site to 5.1.html)

2. In [Start] [Administrative Tools] [DataSources (ODBC)] go to the [System DSN] tab and add a new datasource for your database. If you run into trouble, the detailed docs are here: http://dev.mysql.com/doc/refman/5.0/en/myodbc-connector.html

3. Edit your httpd.conf file to enable DBD access. This example presumes you named your ODBC datasource "mySQLodbc" in step 2, but you can use any name for your datasource.Code: 
LoadModule dbd_module modules/mod_dbd.so 
DBDriver odbc 
DBDParams "DATASOURCE=mySQLodbc" 
DBDKeep 10 
DBDMax 10 
DBDMin 10

See the mod_dbd docs at http://httpd.apache.org/docs/2.2/mod/mod_dbd.html and the ODBC driver docs at http://code.google.com/p/odbc-dbd/wiki/Windows for more details. You can skip the Compiling and Installing apr_dbd_odbc section in the ODBC driver docs - it's already done.

Hope this saves someone some time!
Andynic


----------

