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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MYSQL connection from PERL

Status
Not open for further replies.

NashTrump

Technical User
Jul 23, 2005
38
GB
Hi there,

Im new to perl.

Ive manged to install perl and some modules... i want to create a connection to mysql.
Ive install DBI and DBD-Mysql

However when i try to run the below script:

#!/perl/bin/perl
use Mysql;

$database = 'sphider_db';
$user = 'root';
$password = '';

$dbh = Mysql->connect(undef, $database, $user, $password);
$dbh = Mysql->connect($host, $database, $user, $password);


It comes up with the error:

Undefined subroutine &DBD::mysql::constant called at C:/Perl/site/lib/Mysql.pm line 236.
Compilation failed in require at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databasemysql.pl line 3.
BEGIN failed--compilation aborted at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databasemysql.pl line 3.

I dont understand what this means...

Any ideas?

ive also tried to use DBI in the below script:

use DBI;

# connect
my $dbh = DBI->connect("dbi:mysql:sphider_db:localhost:3306", "root", "");

however this comes up with the error:

Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI"?) at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 6.

So irritating!!

Im pretty sure ive installed DBI correctly.. how do i check that its installed correctly?

Kind regards

Nash
 
You do it this way
Code:
#!/perl/bin/perl -w

use strict;
use DBI;

my $data = "sphider_db";
my $host = "localhost";
my $user = "root";
my $pass = "password";

my $dbh = DBI->connect("DBI:mysql:$data:$host", $user, $pass);

M. Brooks
 
Hi there mbrooks..

i now get this error:

Global symbol "$db" requires explicit package name at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 13.
Global symbol "$database" requires explicit package name at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 13.
Global symbol "@tables" requires explicit package name at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 15.
Global symbol "$db" requires explicit package name at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 15.
Global symbol "$table" requires explicit package name at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 17.
Global symbol "@tables" requires explicit package name at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 17.
Global symbol "$table" requires explicit package name at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 18.
Execution of C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl aborted due to compilation errors.
 
O.K. that is a totally different issue. You are not declaring your variables with my like I did in the example above.

You should always use strict; and -w when programming.

In your case though, for the time being, remove these and your script should work.

Further reference.

M. Brooks
 
im still getting the error message
Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI"?) at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 11.
 
Yes i copied exactly what you wrote above and pasted it into my script..(obviously changed my user and passwords to fit)

#!/perl/bin/perl -w

use strict;
use DBI;

my $data = "sphider_db";
my $host = "localhost";
my $user = "root";
my $pass = "password";

my $dbh = DBI->connect("DBI:mysql:$data:$host", $user, $pass);
 
I think the DBI was not installed correctly. This should work.
Code:
#!/perl/bin/perl -w

use strict;
use DBI;

my $data = "sphider_db";
my $host = "localhost";
my $user = "root";
my $pass = "password";

my $dbh = DBI->connect("DBI:mysql:$data:$host", $user, $pass)
	or die $DBI::errstr;

M. Brooks
 
I got this:

Name "DBI::errstr" used only once: possible typo at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 12.
Can't locate object method "connect" via package "DBI" at C:\Documents and Settings\Lee Nash\Desktop\GAF Projects\Betting\Perl\databaseaccess.pl line 11.

How do i reinstall DBI?
Im guessing i must have incorrectly installed it then...
 
That's the way i originally installed it.
Do i have to do anything manually?

I've uninstalled dbi and dbd-mysql and reinstalled them both using the PPM.
Yet still i get the same problem....

Is there something under perl/bin/perl that i have to do in order to make the script look for the mysql.pm or anything??
 
thanks mBrooks!

It turned out to be perl/site/lib!!

Thats where i was going wrong!

Thanks again.. your a star..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top