in reply to Return values from $dbh->do vs. $sth->execute();

Both do() and execute() should return undef in the case of an error.

The thing to realize is that an UPDATE or DELETE that affects 0 rows is not an error, as far as both DBI and MySQL are concerned. This is why you get the 0E0 return value instead of 0: the operation succeeded, but didn't affect any rows.

Having said all that, you should still be using placeholders for UPDATE/DELETE/INSERT statements. You can do that with do() if you want.

Michael

Replies are listed 'Best First'.
Re: Answer: Return values from $dbh->do vs. $sth->execute();
by injunjoel (Priest) on Nov 24, 2003 at 23:46 UTC
    Thank you for the quick response
    I am afraid though, that you misread my question.
    The issue I am having is not between "0E0" vs "0" being returned (that was a side note to indicate that I have already accounted for that), my issue is that the number of rows affected by the query is not being consistently returned. With $dbh->do I get a consistent return value but I cannot prepare with ?'s. If I prepare the statement and assign it to a statement handle my $sth = $dbh->prepare($query) || die $dbh->errstr; I cannot count on getting the number of rows affected. Is this consistent with DBI? or does this behavior seem specific to my system setup? Is there a property of the $dbh I should be checking?

    You also mentioned utilizing placeholders ? within a $dbh->do() call, where could I find information regarding that, or would you be so kind as to offer an example?

    Like I hopefully mentioned earlier this is not a critical issue for me but I figured I should post it as a question in case there was someone else who has seen this behavior and had already found the explaination/solution. Thank you again
    -injunjoel
      For placeholders with do():
      # Execute a delete from table foo where bar = "bars_value": $dbh->do("delete from foo where bar = ?", undef, "bars_value");
      For the return codes, the DBI spec says that do() and execute() should return the same thing - that is the number of rows affected by the operation, if that information is available, or -1 if the number of rows affected is unknown.

      Now I don't know much about MySQL, or about DBD::mysql, so there may be something special going on here. You might want to check the DBD::mysql documentation directly.

      Michael