3

I have very limited knowledge about server-side development. I am trying to do a project for school. I have to use perl to write a script that will access a table in MySQL. Using XAMPP on windows 7 for server.

The client is just a simple button for now

<html>
  <body>
    <form method="POST" action="/cgi-bin/perltest.cgi">
       <input type="submit" value="Test">
    </form> 
  </body>
</html>

If I use XAMPP's default perltest.cgi which is

#!"C:\xampp\perl\bin\perl.exe"

print "Content-type: text/html\n\n";
print '<html>';
print '<head>';
print '<meta name="author" content="Kay Vogelgesang">';
print '<link href="/xampp/xampp.css" rel="stylesheet" type="text/css">';
print '</head>';
print "<body>&nbsp;<p><h1>GCI with MiniPerl</h1>";
print  "CGI with MiniPerl is ready ...</body></html>";

it works fine. The problem starts when I write my own test.cgi to try to use DBI module to connect to my database.

The sample code for the database connection was provided by my teacher.

#!"C:\xampp\perl\bin\perl.exe"

 use DBI;

 $dbh = DBI->connect('dbi:mysql:perltest','root','')
     or die "Connection Error: $DBI::errstr\n";
 $sql = "select * from test";
 $sth = $dbh->prepare($sql);
 $sth->execute
    or die "SQL Error: $DBI::errstr\n";
 while (@row = $sth->fetchrow_array) {
     print "@row\n";
 } 

Whenever I try to run this, I get the error

Server error!

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

If you think this is a server error, please contact the webmaster.

Error 500

127.0.0.1
Apache/2.4.29 (Win32) OpenSSL/1.0.2l PHP/7.1.11

Apache's error log gives me the following message

[Sat Nov 18 00:36:56.126755 2017] [win32:error] [pid 7420:tid 1620] [client 127.0.0.1:50370] AH02102: C:/xampp/cgi-bin/test.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line, referer: http://127.0.0.1/test.html
[Sat Nov 18 00:36:56.126755 2017] [cgi:error] [pid 7420:tid 1620] (9)Bad file descriptor: [client 127.0.0.1:50370] AH01222: don't know how to spawn child process: C:/xampp/cgi-bin/test.cgi, referer: http://127.0.0.1/test.html

As you can see I have used #!. I have no idea where I am going wrong.

hungryhippo
  • 83
  • 1
  • 7
  • As a first test, I'd copy the working `perltest.cgi` to `test.cgi` (overwriting it) and see whether `test.cgi` then works. If it works, then I'd replace the `print` lines with the content of `test.cgi` and again, see whether it works. Don't touch the first line (the so called "she-bang line"). Make sure there's no empty line before it. The very first line must be `#!...`. – PerlDuck Nov 17 '17 at 20:27
  • Tried it two ways: 1) Copied the entire contents of test.cgi into perltest.cgi. That resulted in the same error 2) Copied everything but the first line into perltest.cgi. That resulted in a prompt `the program can't start because libmysql_.dll is missing from your computer`. (The underscore is not a typo). There was no line before the `#!...`, but there was a space. Erased that to no avail. – hungryhippo Nov 17 '17 at 21:01
  • Update: Turns out whitespace _was_ the problem. Deleted all the whitespace from the beginning of each line and now test.cgi _also_ says libmysql_.dll missing. – hungryhippo Nov 17 '17 at 21:03
  • That's (kinda) good news. So you are lacking the MySQL libraries on your computer but in general your script works. – PerlDuck Nov 17 '17 at 21:16
  • I fixed it! Downloaded Strawberry Perl and used it's libmysql_.dll (https://stackoverflow.com/questions/20990786/perl-mysql-error-libmysql-dll), which did not work. Finally gave up and used Strawberry Perl's exe file instead of the xampp one. Still can't get it to print each row in a newline, but I'll take what I can get – hungryhippo Nov 17 '17 at 21:58
  • Glad to hear. Now you have to combine both scripts. Print your DB results in a
    ...
    section.
    – PerlDuck Nov 17 '17 at 22:13

0 Answers0