Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Basic error in CGI script

by sanPerl (Friar)
on Dec 09, 2009 at 19:39 UTC ( #812015=perlquestion: print w/replies, xml ) Need Help??
sanPerl has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks
I am learning CGI with MySQL connection. I am using following CGI script, which I got from some tutorial
#!/usr/bin/perl -wT print "Content-type: text/html \n\n"; use DBI; # Connect To Database $database = "a_qtest"; $username = "a_read"; $password = "password"; $hostname = "localhost"; $db = DBI->connect("DBI:mysql:$database:$hostname", $username, $passwo +rd); if(!$db = DBI->connect("DBI:mysql:$database:$hostname", $username, $pa +ssword)) print "Connection unsuccessful. Please check your login credentia +ls. ".$DBI::errstr; # Execute a Query $query = $db->prepare("SELECT * FROM mytable"); $query->execute; # How many rows in result? $numrows = $query->rows; # Display Results while (@array = $query->fetchrow_array) { ($field1, $field2, $field3) = @array; print "field1 = $field1, field2 = $field2, field3 = $field3 "; } # Cleaning Up $query->finish; $db->disconnect; exit(0);
I am getting following error
Internal Server Error The server encountered an internal error or misconfiguration and was u +nable to complete your request. Please contact the server administrator, and infor +m them of the time the error occurred, and anything you might have do +ne that may have caused the error. More information about this error may be available in the server error + log. Additionally, a 404 Not Found error was encountered while trying to us +e an ErrorDocument to handle the request. ---------------------------------------------------------------------- +---------- Apache/2.2.13 Server at Port 80
I would be grateful if anyone provides me any clues.

Replies are listed 'Best First'.
Re: Basic error in CGI script
by afoken (Monsignor) on Dec 09, 2009 at 19:47 UTC

    Well, do what the web server told you: Look into the error log, there you will find a more useful message.

    On a web server NOT connected to the internet, you could also insert use CGI::Carp qw(-fatalsToBrowser); into your script. But NEVER do that on a server connected to the internet, it exposes way too much information for a malicious attacker.

    Taint mode (-T) is a very good idea, warnings (-w) are too, but your code lacks use strict;. Add that to your code.

    Also note that Perl requires a block after if, unlike C.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Basic error in CGI script
by ww (Bishop) on Dec 09, 2009 at 19:45 UTC
    • You can safely ignore the last paragraph (for now)
    • You should act on the penultimate note.
Re: Basic error in CGI script
by vitoco (Friar) on Dec 09, 2009 at 21:51 UTC

    Not related with your current problem, but to prevent obscure problems in the future, never SELECT * FROM any table. Use explicit list of fields on every query...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://812015]
Approved by ww
and the monks are chillaxin'...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2017-03-01 18:33 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (428 votes). Check out past polls.