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

Hi Monks,
This code gets a value from another part of my program, as you can see I am just trying to pass a variable value into this subroutine, and when the program runs it ignores de $sql and the DB connection doesn't work. It generates an error like:
2002-04-09<[Thu Nov 11 09:39:18 2004] assignment.pl: DBD::ODBC::st fet +chrow_hashref failed: (DBD: no select statement currently executing err=-1) at assignment_c. +pl

I am trying to understand why the code retrieving the information is actually causing the sub routine not to work, but if a comment out the line:
my ($all_info) = shift (@_);

The program runs fine.
Thanks for the help!
Here is the code
There is more to the code, but all I would like to know is why this line is causing all my problems.
my ($all_info) = shift (@_);
sub C58MASTP { my $check; # retrieving argument my ($all_info) = shift (@_); if($X_NUM){$check=$X_Y;} if($all_info){$check=$all_info;} $sql= "SELECT C.mast1, C.mast2 FROM MASTER WHERE C.mast1=$check AND C +.mast2=$X_Y"; }

Replies are listed 'Best First'.
Re: Value to Sub
by ikegami (Patriarch) on Nov 11, 2004 at 15:15 UTC

    Check the result of prepare and execute. I bet you're getting a syntax error, and the fetch is giving you that error. More specifically, you're not escaping $X_Y and $check, and you're not quoting $check. I prefer replaceable parameters instead of building a SQL string dynamically where possible. If you use replaceable parameters, you don't have to quote or escape, and you're safe from SQL injection attacks.

    $sql= "SELECT C.mast1, C.mast2 FROM MASTER WHERE C.mast1=? AND C.mast2 +=?"; $sth = $dbh->prepare($sql) or die(...); $sth->execute($check, $X_Y) or die(...); ...

      Another possible problem is that the OP uses the table alias C, but never defines it anywhere that I can see (no pun intended.) Perhaps what was meant was:

      $sql= "SELECT C.mast1, C.mast2 FROM MASTER AS C WHERE C.mast1=? AND C.mast2=?";
Re: Value to Sub
by ysth (Canon) on Nov 11, 2004 at 19:41 UTC
    The line my ($all_info) = shift(@_) assumes you are passing what you want to go into $all_info to C58MASTP, like C58MASTP("my_all_info").

    You don't show us how you call C58MASTP, so I don't know what's going wrong. Perhaps you should print "all_info: $all_info X_Y: $X_Y X_NUM: $X_NUM\n"; before your select and make sure all is as you think.