Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Cannot run a simple script?help im new

by ginda (Initiate)
on Mar 01, 2005 at 05:05 UTC ( [id://435285]=perlquestion: print w/replies, xml ) Need Help??

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

I have active perl installed, the latest http apache and the DBI for mysql. All this on a windows xp machine.
I have managed to read and write to mysql tables but i jumped back a little and started again and tried to run this code:


#!c:/perl/bin/perl -wt use strict; use CGI; $Query = new CGI; use DBI; $dbh = DBI->connect("DBI:mysql:exelstock") or die "Cannot connect: " . $DBI::errstr; $Value = $Query->param('Value'); $sth = $dbh->prepare("SELECT * FROM stock WHERE name='$Value'"); $sth = $dbh->execute(); print "Content-type: text/html\n\n"; print "<HTML>\n"; print "<BODY>\n"; while (@results = $sth->fetchrow_array) { print "@row\n"; } $sth = $dbh->finish(); $dbh->disconnect(); print "</BODY>\n"; print "</HTML>\n";



all i keep getting is 500 internal error, i cannot understand why this is happening???

20050301 Janitored by Corion: Put code in code tags

Replies are listed 'Best First'.
Re: Cannot run a simple script?help im new
by Zaxo (Archbishop) on Mar 01, 2005 at 05:28 UTC

    500 errors mean the script couldn't complete, or failed to provide http headers to httpd. You are printing the correct header, so that's not the issue.

    You have weak taint mode on, but you haven't detainted $Value. With warnings and strict on, the warnings from that will cause perl to not execute. Also, by using strict, you need to declare your variables lexical with my, or explicitly global with use vars . . .; or our. Again, that will prevent execution.

    You are giving only one argument to DBI::connect. Maybe you just erased the $user and $passwd args before posting (wise!), but if they are missing in the real script the connection will fail.

    You should also make sure that values fed to $sth are correctly quoted. You have attempted to do so with single quotes, and that might work for most $Values, but it is cleaner to use a placeholder,

    $sth = $dbh->prepare("SELECT * FROM stock WHERE name=?"); $dbh->execute($Value);

    You should check to see if the database connection succeeds. Testing definedness or truth of $dbh is sufficient for that.

    After Compline,
    Zaxo

Re: Cannot run a simple script?help im new
by jpeg (Chaplain) on Mar 01, 2005 at 05:26 UTC
    First off, you should check the logs. Apache's error_log and mysql's (hostname).err and (hostname).query.log are very helpful. I bet if you turned on the query log and watched it, you'd see that the script isn't connecting at all.

    Regarding the script, I don't see a dsn specifying the host or port, and I don't see a database handle specifying a username or password.

    Hope this helps.

Re: Cannot run a simple script?help im new
by PodMaster (Abbot) on Mar 01, 2005 at 05:29 UTC
    Visit Tutorials and read CGI Help Guide, as it will guide you through figuring out why you're getting 500 internal error.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Cannot run a simple script?help im new
by Nevtlathiel (Friar) on Mar 01, 2005 at 08:59 UTC
    You do have permission to execute the script right? That one always gets me when I start on a new script ;)
Re: Cannot run a simple script?help im new
by matthewb (Curate) on Mar 01, 2005 at 11:57 UTC
    all i keep getting is 500 internal error, i cannot understand why this is happening???

    You've received some good advice and clearly some kind of CGI tutorial would help you out.

    The obvious reason that your script won't compile, however, is that you are using strict (recommended) but not then using my to declare your variables.



    MB
      Thanks all for your help
      I have tried to implement what you guys have advised heres the code:

      #!c:/perl/bin/perl -wT

      use strict;
      use CGI;
      use CGI::Carp qw(fatalsToBrowser);
      use vars qw($Query $sth $dbh $Value @results @row);
      $Query = new CGI( );
      use DBI;
      $dbh = DBI->connect('DBI:mysql:exelstock')
      or die "Cannot connect: " . $DBI::errstr;

      $Value = $Query->param('input');


      $sth = $dbh->prepare("SELECT * FROM stock WHERE name=$Value");
      $sth = $dbh->execute();
      print "Content-type: text/html\n\n";
      print "<HTML>\n";
      print "<BODY>\n";
      while (@results = $sth->fetchrow_array) { print "@row\n";
      }
      $sth = $dbh->finish();
      $dbh->disconnect();
      print "</HTML>\n";
      print "</BODY>\n";


      Heres the error i get now...any ideas???

      Can't locate object method "execute" via package "DBI::db" at c:\PROGRA~1\APACHE~1\apache\cgi-bin\project2.cgi line 23.

        That's simply because, as the error message says, DBI::db doesn't have an execute method. Try changing it from $sth = $dbh->execute(); to $sth->execute();

        Also, please use code tags when posting code. Thanks.

Re: Cannot run a simple script?help im new
by chas (Priest) on Mar 01, 2005 at 05:47 UTC
    In your code: #!c:/perl/bin/perl -wt
    In addition to what everyone else said, -t doesn't seem to be a valid switch; you need -T for taint mode. (-t gives an error even on Windows.)
    chas
    (Update: Sorry, the -t switch produces an error in perl v5.6.1, but not in v5.8.0. My mistake...unless the original poster is using a version where -t isn't recognized...)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found