Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

read database 500 error

by PaganJim (Initiate)
on Aug 08, 2021 at 20:02 UTC ( [id://11135713]=perlquestion: print w/replies, xml ) Need Help??

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

Hello everyone. I have been away from PERL for quite a while, a few years actually. But I was looking at an old script that I wrote that I want to resurrect from the dead, so to speak. The old script was basically to help me learn now I am using that way again but it will go back up on the web at some point. When I originally wrote it, I didn't need a database, I just had a small amount of data that I kept in txt files. But now, I am expecting a lot more data so I am trying to move it to a db. I have a db and table populated with info. So I am trying to write a script to read the db line by line using fetchrow_array. But for some reason the script keeps popping a 500 error and there is nothing in my log files.

So the script so far seems pretty straight forward but I cannot for the life of me find the error. And no errors show up in my error logs and no error is displayed. Anyone see anything? All feedback greatly appreciated since I am just getting back into perl.

#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use DBI; ## mysql user database name $db ="dbname"; ## mysql database user name $user = "dbuser"; ## mysql database password $pass = "abc123"; ## user hostname $host="localhost"; my $dsn = "DBI:mysql:database=$db;host=$host"; my $dbh = DBI->connect($dsn, $user, $pass) or die("Error connecting to the database: $DBI::errstr\n"); my $sql = qq/select * from table/; my $sth = $dbh->prepare ($sql); while (@row = $sth->fetchrow_array) { my ($a, $b, $c, $d ) = @row; print ("--", @row), "\n\n"; } $dbh->disconnect (); exit (0);

Replies are listed 'Best First'.
Re: read database 500 error
by haukex (Archbishop) on Aug 08, 2021 at 20:14 UTC
    But for some reason the script keeps popping a 500 error and there is nothing in my log files.

    When running a script as a CGI script, it needs to output headers before anything else. Please see one of the alternatives to CGI.pm for which modules you could use. As one example, you might try:

    use CGI::Simple; my $q = CGI::Simple->new; print $q->header();

    In general, Use strict and warnings, and see also Basic debugging checklist, CGI Help Guide, and Troubleshooting Perl CGI scripts. Note you can always run your CGI script from the command line first to see any errors.

    Update: Also, note print ("--", @row), "\n\n"; is not doing what you think, and warnings would have given you some hints about that: it's being interpreted as ( print("--", @row), "\n\n" ); - that is, print is only getting the arguments "--", @row, but not "\n\n". You need to write print "--", @row, "\n\n"; or print(("--", @row), "\n\n"); instead. And it's best not to use $a and $b as variable names as those get special treatment by sort.

      When running a script as a CGI script, it needs to output headers before anything else. Please see one of the alternatives to CGI.pm for which modules you could use. As one example, you might try: use CGI::Simple;

      CGI::Simple is just an old stripped version of CGI.pm. CGI.pm is king of that old gateway tech. CGI::Alternatives explains

Re: read database 500 error
by Bod (Parson) on Aug 08, 2021 at 21:31 UTC
    And no errors show up in my error logs and no error is displayed

    That's because the script is not generating an error, the CGI environment is. As haukex says, you need to output some headers.

    Simply adding:

    print "Content-type: text/plain\n\n";
    somewhere before you print the output should solve the problem.

    Generally if you include use CGI::Carp qw(fatalsToBrowser); the only reasons for a 500 error will be that the script cannot start (permissions or other issues) or it is not printing headers first.

Re: read database 500 error
by dsheroh (Monsignor) on Aug 09, 2021 at 07:25 UTC
    But for some reason the script keeps popping a 500 error and there is nothing in my log files.
    For the sake of completeness, since neither of the other answers so far mentioned this: A 500 error will be logged in your apache (or other HTTP server) error log rather than your application's own log files. Probably "Premature end of script headers", because no valid HTTP header was provided by your code.
      Hi everyone. Apologies but I have been off the computer for a couple of days. As they say, life is what happens when you are making other plans. I hate it when you can't get online and you come back to 100+ emails. Anyway, unfortunately for some reason my server is not generating ANY error messages for some reason. But I put the headers in and the script worked with no other changes. But, just since I know how any computer, especially servers it seems, can be I put it back in and no 500. But I want to thank everyone for the help, suggestions and links provided. I am sure that they will all be very useful to me since I am trying to resurrect this old script. So, thanks again and everyone have a wonderful day! Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11135713]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-28 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found