Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

HTTP Error 502.2 - Bad Gateway in Perl CGI script when accessing SQL Stored Proc with Parameters

Posted on 2009-05-20
3
Medium Priority
?
1,095 Views
Last Modified: 2013-12-07
Hello,

I am currently involved in a project that requires me to send some data from a SQL database in a Perl script file.

Having never delt with Perl this is proving slightly problematic as I don't really know what I am doing but I am figuring it out slowly.

So far I have managed to get the connection to work correctly and can run basic SELECT commands using the URL parameters. I have also managed to get a SQL Stored Proc to run fine with out any problems that contained a simple SELECT * FROM Table statement.

The problem I am having now is adding the Parameters to the file as I keep getting a 502.2 error and I can't work out why.

I have attached a code snippet below and hopefully it is something glaringly obvious but as I have no Perl experience I have no idea. I have tried using some example in tutorials but I keep getting the error.

Lee
#!/usr/bin/perl
use CGI qw(param);
my $Model = param("Model");
 
print "Content-type: text/html\n\n";
 
print "<html><body>";
 
print "<br><p><center><h2>Data Return (CGI)</h2></center></p>";
 
print "<p>Start Of Data for Model: $Model</p>";
 
print "<p>";
 
use strict;
use DBI;
my $db_user = "user";
my $db_pass = "password";
if (my $dbh = DBI-> connect('dbi:ODBC:perltest',$db_user,$db_pass)) {
 
 
   my $sql = "declare @Model char(6) exec mtkSP_PerlTest @Model =123456";
 
   if (my $sth = $dbh->prepare($sql)) {
 
       if ($sth->execute()) {
 
           while (my @row = $sth->fetchrow_array) {
 
               print join(", ", @row), "!";
           }
       }
       else {
 
           print "Execute failed : ".$dbh->errstr;
       }
   }
   else {
 
       print "Prepare failed : ".$dbh->errstr;
   }
}
else {
 
   print "Failed to login";
 
}
 
print "</p>";
 
print "<p>End Of Data</p>";
print "</body></html>\n";
 
exit;

Open in new window

0
Comment
Question by:Lee Redhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 80 total points
ID: 24436182
What web server are you using?  Is there anything in your error log?
Try adding this after your line 2... what is the output:
use CGI::Carp 'fatalsToBrowser';

Open in new window

0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 1920 total points
ID: 24436557
I assume the "declare @Model " is meant to be parsed by SQL Server, however, @Model will be interpolated by Perl as a list variable. You need to either escape it, or use single quotes in the SQL string.

# Try single quotes

my $sql = 'declare @Model char(6) exec mtkSP_PerlTest @Model =123456';

#OR escape the @

my $sql = "declare \@Model char(6) exec mtkSP_PerlTest \@Model =123456";
0
 

Author Closing Comment

by:Lee Redhead
ID: 31583658
mrjoltcola - That worked perfectly, brought back the data as expected.

adam314 -Even though it didn't answer the question being able to see what has gone wrong is really useful.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question