Right. First, I think the logic is flawed. We should successfully prepare() or die. Period. If the call to prepare() failed ($sth is undef), we should not making dying conditional on yet another value.

More to the point, this line is actually the cause of the problem. (Sorry I didn't see it earlier.) You've run into the precedence rules:

  my $sth = $dbh->prepare( $sql ) or die $dbh->errstr if $dbh->err;

is read as

  (my $sth = $dbh->prepare( $sql ) or die $dbh->errstr) if $dbh->err;

That is, it is equivalent to

  if ($dbh->err)
  {
    $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
  }

Since the connect succeeded, $dbh->err is undef, so we never even call prepare! Hence, $sth is undef when we get to execute, and you get the error message. I expect this is what Joe (John Doe) was trying to tell us earlier.

The simplest solution would be to drop the "if $dbh->err".  That is, change to

  my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;

John's suggestion (below) is better still, as it adds helpful detail to the error message when there is one (though I don't see the need to make it a separate line of code).

Michael


John Trammell wrote:

Gerald Preston wrote:
[snip]

my $sth = $dbh->prepare( $sql ) or die $dbh->errstr if $dbh->err;

[snip]

Regardless of other problems you may be having, I think you're not
doing what you want to do here.  How about instead:

my $sth = $dbh->prepare($sql);
$sth || die "Error preparing sth from '$sql': ", $dbh->errstr;


-- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]



Reply via email to