PDA

View Full Version : Perl and mySQL, need help



kennymoens
September 26th, 2001, 11:47
Hi all,

I'm running a mySQL database, its correctly set up with phpMyAdmin, but since all my scripts are in Perl I would like to access my database via Perl. I've done this, but I always get a 500 error, so I think there must be an error in my code.

Here is the code:


# Connection to the Database #
$DSN = "DBI:mysql:database=sabenava_timetable";
$user = "ain't giving to you";
$pw = "ain't giving to you";
$dbh = DBI->connect($DSN,$user,$pw) || die "Connection Failed";

# Executing the command #
my $query="SELECT cat,flightnr,dep,arr,icaodep,
icaoarr,freq,aircraft,deptime,flighttime,arrtime FROM timetable";
my $sth=$dbh->prepare($query);
$sth->execute || die "Could not execute SQL
statement ... maybe invalid?";


# Declare the Variables #
my ($dbcat,$dbflightnr,$dbdep,$dbarr,$dbicaodep,$dbicaoarr,
$dbfreq,$dbaircraft,$dbdeptime,$dbflighttime,$dbarrtime);

print "Content-type: text/html \n\n";
print qq~
<html><head>
<title>Database</title>
</head><body>
~;

# Collect the data #
while ( ($dbcat,$dbflightnr,$dbdep,$dbarr,$dbicaodep,
$dbicaoarr,$dbfreq,$dbaircraft,$dbdeptime,$dbflighttime,
$dbarrtime) = $sth->fetchrow() ) {
print "$dbcat";
}

# Gegevens Afdrukken #
print qq~
</body></html>
~;

$dbh->disconnect;


(Note: some things need to be at the same rule)
Hope you see the error,

thx for the help

jm4n
September 26th, 2001, 20:39
First thing first: have you checked your error logs? Have you run 'perl -c filename.pl' to see if there is a parse error somewhere?

I'm assuming you posted only the relevant parts, but did you forget 'use DBI'? If not you'll see something about this in your error logs.

kennymoens
September 27th, 2001, 04:32
1) I checked the error logs: the only thing there is "Error in line 32 from script timetablenew.cgi", this is the line for the connection string, the only thing, on my server page it says, "use this line to connect to the db".

2) I checked the script for errors with DZPerl Editor (with ActivePerl installed).

3) I don't forgot "use DBI;" :)

BTW: The 500 error is gone now, the script runs, but everything before the connection works, but anything after it, doesn't work.

jm4n
September 27th, 2001, 04:53
Now that I have that extra bit of information (namely, the specific line that died), I can give you some advice:
I haven't done much with DBI, but from what I can tell, the data source should simply be:

$DSN = "DBI:mysql:sabenava_timetable";

(without the "database=" part).

However, I wouldn't imagine that would crash it out without any error message, it seems it should just cause the connection to fail (and thus you should see your "Connection Failed" in the log)...

Now, you mention ActivePerl -- this is a Windows thing, right? Are you doing this under Windows, or are you just refering to your test environment (eg, home PC)?

If you're developing a Perl/MySQL script for use under Windows... you're on your own :) But if (as I suspect) you simply mean you did a syntax check on a Windows box, I'd have to advise doing the syntax checking on the same machine it will be run on -- especially when it comes to perl modules such as DBI being used. Slight version differences (or in this case, major platform differences) can make a difference.

kennymoens
September 27th, 2001, 05:05
OK, I've tested it without the "database=" part, doesn't work either.

With ActivePerl I ment, my test machine at home. My server runs Unix.

How can I check the exact error logs?
Since I only check the "error log" page in my Control Panel from the server.

jm4n
September 27th, 2001, 05:11
You'll need to contact your administrator to find out where your error logs are located (and to see if you even have access to them; most hosts do provide this though). Anything printed on stderr (eg, from a die() or a Perl error message) will generally be logged in your web-server's error log (eg, Apache's error log).

What it sounds like might be happening is your control panel is showing you only the last line of the log entry... Perl error messages typically give an error, then a final line saying "execution failed on line xx"...

madsurfer
September 28th, 2001, 04:11
You could also try putting

use CGI::Carp qw(fatalsToBrowser);

near the start of your code, this will put stderr messages to your web browser, so you shouldn't need to access your error logs

kennymoens
September 28th, 2001, 04:20
OK guys,

I got access to the error logs, found the error, there was something wrong with the DBI, its solved now.

Thanks