Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

DBI script gives HTTP 500 error

by peppiv (Curate)
on Jan 15, 2002 at 01:28 UTC ( #138712=perlquestion: print w/replies, xml ) Need Help??

peppiv has asked for the wisdom of the Perl Monks concerning the following question:

"I need a drink!" is the last thing I said outloud while trying to get this perl/DBI script to work. Yes, I'm a delinquent perl hacker and I need help.

Here's the prob.

I can't get this perl script to run. I get an HTTP 500 internal server error everytime I run it. Here's the code:

#!/usr/bin/perl -w use strict; use DBI; print "Content-type: text/html\n\n"; $dbh = DBI->connect(qq{DBI:CSV:Book1:csv_sep_char=\\|}, {RaiseErro +r => 1} ); $dbh->{'csv_tables'}->{'Book1'} = {'file' => 'csv'}; $dbh->{'col_names' => ["timestamp", "email", "name", "address", "c +ity", "state", "zip"]}; $sth = $dbh->prepare("SELECT * FROM Book1 WHERE city = ?") or die +"Cannot prepare: " . $dbh->errstr(); $sth->execute('Orlando'); while (my @result = $sth->fetchrow_array()) { print "@result\n"; } $sth->finish(); $dbh->disconnect();
My strict is up to date. I installed DBI, DBD:CSV. My file is Book1.csv and it's in the same directory as the script. I want it to print to screen all matches where city = Orlando (we're looking for a new basketball team owner). I'm running Apache/FreeBSD. Uploaded in ASCII chmod to correct permissions.

This is my first DBI connection so I've never worked one yet. Help!


p.s. I've gotten some good help from the Monks and they've helped me get this far. Can't find anything else in docs to get me past this hump. Thanks

Replies are listed 'Best First'.
Re: DBI Help!
by runrig (Abbot) on Jan 15, 2002 at 01:33 UTC
    This looks like a good place to start. Or do a Super Search on "500 internal server error" and there are plenty of useful links like one advising use of fatalsToBrowser with CGI::Carp...

    Also, you might know that your file is in the same directory as your script, but your program doesn't know that, so you should probably tell it where your program is (i.e. give the file a full absolute path).

      Thanks runrig.
      fatalsToBrowser yields:
      Execution of /cgi-bin/ aborted due to compilation errors.
Re: DBI Help!
by tradez (Pilgrim) on Jan 15, 2002 at 01:55 UTC
    I am going to just give a syntax help I have learned through the years, if you get an apache error, have a terminal running with a tail -f /dir/of/apache/error_log that will grow as you refresh the page. Usually you will see a very simple error that you would miss by simply adding a -w or perl -c'ing your code. Helps me a lot in debugging. You can also within this do some
    warn "Got Here \n";
    throughout the code to see the point at which the code is bombing. Hope this helps. tradez
Re: DBI Help!
by jonjacobmoon (Pilgrim) on Jan 15, 2002 at 02:45 UTC
    I don't know if anyone mentioned this or I am off base, but the line:
    $dbh->{'col_names' => ["timestamp", "email", "name", "address", "city" +, "state", "zip"]};
    Maybe should be:
    $dbh->{'col_names'} = ["timestamp", "email", "name", "address", "city +", "state", "zip"];
    I have never used csv in DBI before but the book says that $dbh{'col_names'} is array. You have a hash style pointer ( => ) with array style brackets ( ).

    At the very least, my change causes perl -c to come back ok.

    I admit it, I am Paco.
      Thanks for the eagle eye. I changed it, but to no avail.
      Where's that drink!
Re: DBI Help!
by andye (Curate) on Jan 15, 2002 at 03:03 UTC
    perl -c will tell you about any compile-time errors.

    FatalsToBrowser is sometimes, well, a bit vague about an error, say where you'd normally get a little code excerpt. It's for Security Reasons (or, as they sang in Salad Days, "Shh! It's hush-hush!").


Re: DBI Help!
by higle (Chaplain) on Jan 15, 2002 at 01:45 UTC
    Hmm... ghosty problems like this are the worst.

    Check your HTTP server's error log, and see what the exact error is. This might give a little more insight than the good ole "compilation errors" message.

    Good luck, my brother...
    perl -e 's=$;$/=$\;W=i;$\=$/;s;;XYW\\U$"\;\);sig,$_^=$[x5,print;'
      OK, found the damn error logs. Finally get the message:

      Can't modify constant item in scalar assignment at /usr/local/etc/httpd/cgi-bin/ line 11, near "]}"

      $dbh->{'col_names' = ["timestamp", "email", "name", "address", "city", + "state", "zip"]};

      Any idea what this means? I can't find this error in my docs.
        Did you read jonjacobmoon's post??

        Your reply to his post indicates you did read it and changed your code. What did you do? Change it back? That indicates a lack of basic understanding...I suggest first understanding what your code does. You are trying to assign an array reference to a string constant...not a good thing.

Re: DBI Help!
by perrin (Chancellor) on Jan 15, 2002 at 01:41 UTC
    So, what's the error? Look in the error log.
      My apologies. I can't find my error log.
      I can only find my access log.

      The access_log doesn't contain any copies of the error_log. There is no error_log where it used to be.
Re: DBI Help!
by metadoktor (Hermit) on Jan 15, 2002 at 02:54 UTC
    Have you tried to execute your perl script in this manner?
    Doing this will often show you what's wrong with your perl code. If you get valid html output from this then you are doing something else wrong with your script...perhaps having to do with file permissions.


    "The doktor is in."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://138712]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2021-06-15 12:48 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (72 votes). Check out past polls.