connect to mysql database using perl

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
noguru
Forum Commoner
Posts: 61
Joined: Thu Jun 06, 2002 4:03 am
Location: Just north of the City Of Gold, Land of Milk and Honey

connect to mysql database using perl

Post by noguru »

Hi

I need to write a perl script that can do updates on a mysql table, but I haven't really done a lot with perl yet, especially not database stuff. I got this code somewhere on this forum, but can't get it to work. I still want to do major changes, but I need at least to connect to the database. I get an Internal Server Error, in other words there's a syntax error somewhere. (Btw, does anyone know how to see on which line number an error has occured in your perl script, instead of showing "Internal server error"?)

Code: Select all

#!/opt/perl/bin/perl
use DBI;
$dsn = "DBI:mysql:database=mydatabase";
$dbh=DBI->connect($dsn, "myusername", "mypassword");
$sth = $dbh->prepare("SELECT * FROM Leave where Leave_ID=''5''");
$sth->execute;
while( (@results = $sth->fetchrow) != NULL) {
  print "$resultsї'Leave_FromDate']\n";
}
Is the /opt/perl/bin/perl path correct and what is it used for and how will I know what the correct path is?

In the end my objective is to call this perl script from the cron program every first day of each month and add 2 extra leave days for each employee...

Thanks
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

Hey noguru,

Glad to see someone using PERL (its where PHP started :D ). I'll assume you're running perl on some variety of *NIX.

OK, the stuff after #! is specific to your system, so there's no way I can say if its right. It tells the system where to find the program that should execute the rest of the file, in this case perl. You will need to ask your sysadmin for the correct "Path to Perl".

As for checking your script, you can just run it from the command line with:

perl -c -w my_script.pl

The -c flag enables syntax checking, the -w enables warnings, use:

perl --help

to see all the flags. Hope this helps, Jonny.
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

Just had a thought, if you've got the CLI (command line) or CGI version of PHP installed on your system, you might be able to write your script in PHP and run it from cron. Haven't ever tried it, but I can't see why it wouldn't work. Have a look at Using PHP As A Shell Scripting Language on PHPBuilder.
User avatar
noguru
Forum Commoner
Posts: 61
Joined: Thu Jun 06, 2002 4:03 am
Location: Just north of the City Of Gold, Land of Milk and Honey

Post by noguru »

Hi Jonny

Thanx for the reply. Yes I've got command line php installed (apparently) and I have in fact decided to go for a php file and call it from the cron.

It's working fine.
Post Reply