in reply to Re^3: Perl CGI and SQL statements
in thread Perl CGI and SQL statements

if ($dept='CA') is an assignment, not a comparisson. In addition you should use 'eq' for a texual comparison (== for numeric). So:
if ($dept eq 'CA')
is the way to go.
HOWEVER, you have an imbedded 'if' inside a print staement, which is the reason for the syntax error. I suggest you break it up into two print statements.

Replies are listed 'Best First'.
Re^5: Perl CGI and SQL statements
by kjg (Sexton) on Apr 19, 2006 at 12:16 UTC
    Thanks, The snippet now looks like this:
    my $db = new Win32::ODBC('$DSN'); if (defined $dept) { if (exists $labels{$dept}) { # FIXME # untaint $dept and put it into database print $cgi->start_html, $cgi->p("$dept was received."); $cgi->end_html; if ($dept eq "CA") { if (!($db=new Win32::ODBC('$DSN'))) { $cgi->p("Error connecting to Database"); $cgi->p("Error: " . Win32::ODBC::Error ) . " "); } my $SqlStatement ='SELECT * FROM Pipeline WHERE Publish<="19 Apr 2006" + AND Expiry>="19 Apr 2006" AND CA="Must Read"'; print $SqlStatement; $db->Sql('$SqlStatement');
    I'm still getting this error:
    :\Perlcgi\Pipeline\cgi.pl: Can't call method "Sql" on an undefined val +ue at E:\Perlcgi\Pipeline\cgi.pl line 51.
    Line 51 is:    $db->Sql('$SqlStatement'); It's like it won't substitute the contents $SqlStatement or something? Why is that?

    Incidentally, if I drop in the SQL Statement in that Line 51 code it still gives the same error message. Does CGI need me to express the $db->Sql() differently?

      my $db = new Win32::ODBC('$DSN');

      Firstly, you need to either remove the tic marks or use double quotes. The way this is written, you're sending exactly $DSN as a string. If you want to send what $DSN is set to, then use: my $db = new Win32::ODBC($DSN);

      Secondly, this probably explains the next problem... $db is undefined because the instance creation failed. In other words, after you do this:

      my $db = new Win32::ODBC($DSN);

      You should check $db to verify that the object was created successfuly. If it could not connect, then $db will be undefined. The error you are seeing is because you can't call the function Sql on an undefined $db variable.

      Seriously, a little time with the perl debugger, now that you've got compilable code, would help you understand what's going on.

      Neat Debugger tricks

      Re: Linux vs. Windows for Learning Perl

      perl debugger

      Hazah! I'm Employed!

      In order for your variables to be interpolated, you must used double-quotes instead of single quotes (or none at all in this case would do as well).

      $db->Sql($SqlStatement);

      or possibly:

      $db->Sql("$SqlStatement");

      Either of those ought to work. I take it there is no return value to the Sql method, or perhaps you should be capturing that return?


      No good deed goes unpunished. -- (attributed to) Oscar Wilde