in reply to Re: DBI, prepare_cached, finish, and forks
in thread DBI, prepare_cached, finish, and forks

Why thank you!

2. So if I were to call prepare_cached() within the condition (so I'm not preparing the statement if I don't need it, since there are multiple ones), you're saying that I don't need to call finish() on it?

I seem to remember getting errors if I don't call the finish() on my statements... are you saying that as long as I fetch all the returned rows on either a prepare() or a prepare_cached(), I don't need to use a finish()?

I'd assume it's still best practice to use a finish()?
  • Comment on Re^2: DBI, prepare_cached, finish, and forks

Replies are listed 'Best First'.
Re^3: DBI, prepare_cached, finish, and forks
by jZed (Prior) on Feb 24, 2006 at 02:07 UTC

    > I'd assume it's still best practice to use a finish()?

    Nope. From the DBI docs:
    "Adding calls to finish after each fetch loop is a common mistake, don't do it, it can mask genuine problems like uncaught fetch errors."
      Asho!

      I guess I was paying more attention to the:
      "free up resources"
      part of the documentation.

      Will I run into problems using the same variable name $sth in multiple calls without a finish(), like this:
      $sth=$dbh->prepare($sql); $sth->execute; while(($var)=$sth->fetchrow_array) { ... }; $sth=$dbh->prepare($sql2); $sth->execute; while(($var2)=$sthf->fetchrow_array) { ... };
      I'm trying to make sure I understand how to do it right!
        The code you show does *not* present a problem, in other words, it doesn't need finish(). You don't break out of the while loops, which means you do fetch all rows and therefore finish() is called automatically. The only time you need finish is if you fetch less than all rows e.g. you call one of the fetch methods outside of a loop, or if you call it in a loop but break out of the loop before all rows are fetched.