Page 1 of 1

connect to mysql database using perl

Posted: Wed Sep 18, 2002 9:47 am
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

Posted: Fri Sep 27, 2002 7:16 am
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.

Posted: Fri Sep 27, 2002 7:28 am
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.

Posted: Fri Sep 27, 2002 8:02 am
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.