Hi all,

I'm sure this is a simple problem, but hopefully someone will be able to help.

I'm trying to connect to mysql through perl, however when I use the command

use Mysql;

it is giving me an error.
I am assuming it is probably something to do with the directories that things are being ran from.
The specific error is as follows:
can't loocate Mysql.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib)
I am using this in both windows and ubuntu both getting similar errors in both.

If anyone has any ideas please let me know!

Thanks in advance,
N

Recommended Answers

All 6 Replies

There is no "use Mysql". You have to connect to MySQL through DBI/DBD:Mysql. Since you're on windows, you will want to include the uwinnipeg ppm server in your ppm manager. It depends on the version of perl you are running. Go to the command prompt and type perl -v. to get the version. Then type ppm and go to the libraries part of ppm and include: cpan.uwinnipeg.ca/PPMPackages/10xx/ (that's for version 5.10). Then search on DBD MySQL and DBI. Install both. Then you can connect like this:

sub goSQL{
use DBI;
my $statement=shift;
print "$statement\n" if($DEBUG);
#my $dbh,
my $user;
my $password;
my $db;
$database="database" if($database eq "");
$user="root";
my $dbh = DBI->connect("dbi:mysql:$database", $user, $password);
if ($dbh){
		my $id;
		my $count;
		my $color;
		my $sth;
		my @rows;
		my $err;
		$sth=$dbh->prepare("$statement");
		$sth->execute();
		my $nf=$sth->{NUM_OF_FIELDS};
		if ($nf!=0){
			while($id=$sth->fetchrow_hashref()){
				push @rows,$id;
			}
		}
		return \@rows;
	} else {
	    return 0;
	}  
}
1;

That will return a reference to an array with hashes of your query results.

$f=goSQL("select * from table");
for (@$f){
  print "$_->{column_name}\n";  
}

Good luck!

commented: Good example of connect and query. Worked for me. +1

I see mitchems has answered your question for installing in Windows. As for ubuntu, I think I installed the DBD::mysql module the lazy way in ubuntu by using the Synaptic Package manager and doing a quick search for libdbd-mysql-perl. You may already have the DBI module installed as it may have been included with your Perl already. You can read more about installing these modules here.

Ok, thank you for your replies. I am not currently at a machine running Ubuntu so I will get back to that if I am having any problems later.

"Go to the command prompt and type perl -v. to get the version. Then type ppm and go to the libraries part of ppm and include: cpan.uwinnipeg.ca/PPMPackages/10xx/ (that's for version 5.10)."

When I am running the ppm (package manager) I can not find the libraries to include the package you are suggesting.

This is perl, v5.10.1 built for MSWin32-x86-multi-thread
(with 2 registered patches, see perl -V for more detail)

Copyright 1987-2009, Larry Wall

Binary build 1007 [291969] provided by ActiveState http://www.ActiveState.com
Built Jan 26 2010 23:15:11

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

This is my perl -v information so it is 5.10. If you have any more information about where I could locate this package it would be great.

Thanks again.

N

Go to the command line and type "ppm". You might have to go to c:\perl\bin to get it to work if it's not in your path. Then go to the menu under "Properties" and go to repositories. Include the cpan.uwinnipeg.ca/PPMPackages/10xx/ in the ppm manager. Search on DBD Mysql and on DBI. Install both and it should work. If you don't have ppm, I can explain how to install it from the shell or manually.

Hi all,
I had the same problem and i tried the similar way to solve the problem. But am using perl version 5.14.2. How can i install the PPM. Can anyone help me please...?

I don't have Windows on my computer so I can't answer your question if you are asking about installing PPM under Windows. You have a better chance of getting the attention of someone who can help you if you start a new thread with a relevant title such as "Installing PPM (Perl Package Manager) under Windows." (This thread title is "using the 'use Mysql' command" and it's more than a year old.)

If you have a non-Windows platform such as Mac-OS or Ubuntu, please include that information in your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.