Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to MySQL

Status
Not open for further replies.

dalhousi

Programmer
Mar 26, 2007
21
CA
Hi Everyone,

How can I connect with MySQL database using Perl?
Here is the source code I am using:
Code:
#!C:\perl\bin\perl.exe
print "Content-type: text/html\n\n";
use DBI;

$db="perl_test";
$host="localhost";
$port="3306";
$userid="root";
$passwd="";
$connectionInfo="DBI:mysql:database=$db;$host:$port";

$dbh = DBI->connect($connectionInfo,$userid,$passwd);
I checked the log file of Apache and I found the error message:
Code:
install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains: C:/perl/site/lib C:/perl/lib .)
Is there any module I should install and link with Perl? and how can I do so?

Thanks in advance,
 
You have to install DBI module into your file system. The location of this DBI.pm can be C:/perl/site/lib or C:/perl/lib as mentioned in your error log. You can also store it anywhere else and 'use' that path in your script.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I assume you're not really going to use root for this?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for your reply Spookie. Actually, the file exists in right location in "C:/perl/lib/DBI.pm". Also, there is a folder "C:/Perl/lib/DBI". However, it is still not working. Any other suggestions or comments?

Thanks,
 
it's not complaining about DBI.pm it says
Can't locate DBD/mysql.pm
Have you installed the dbd-mysql?
 
Yes, I overlooked the actual error message.
Thanks for correcting travs69.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Thanks for your reply guys. I'm not sure I did the right steps to install dbd-mysql.

I tried to use ppm, the wizard that comes with Perl, but it did not recognize the module dbd-mysql. I tried to download the tar file of dbd-mysql and I copied the files to c:\perl\lib but I keep getting the message "install_driver(MySQL) failed" whenever I try to connect to MySQL database.

Am I on the right track?

Thanks,
 
I just installed dbd-mysql. However, I got the warning:
Code:
Installing DBD-mysql-3.0002 for DBD-mysql-SimpleMySQL would downgrade DBD::mysql from version 4.004 to 3.0002 and Mysql:: from version 4.004 to 1.2401
Anyway, I proceeded.

Now the error message I get has changed to the following:
Code:
DBD::MySQL initialisation failed: Can't locate object method "driver" via package "DBD::MySQL" at C:/perl/site/lib/DBI.pm line 775.

Any suggestions or comments?

Thanks in advance.
 
Problem is solved. I just realized that I was calling DBI:MySQL (capital letters) instead of DBI:mysql.

Thanks everyone,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top