Skip to content

Deal with "forgotten binds" #12

Open
@adrianhardy

Description

@adrianhardy

If we're going to make Statement objects reusable, that means they need to be able to have parameters rebound (which is currently possible).

$stmt = $db->prepare("SELECT * FROM tblTest WHERE intKey1 = ? AND intKey2 = ?");
$stmt->fetchAll(null, [65, 595]); // bind 65 to intKey1 and 595 to intKey2
$stmt->fetchAll(null, [123,123]); // use the statement again with new params \o/

But, what happens if someone calls:

$stmt->fetchAll(null, [321]); // intKey2 is still set to 123 from the previous bind, silly programmer

We have a few improvements:

  • in fetchOne and fetchAll, the array supplied must cover the full number of parameters
  • have a resetParams() method and encourage this before rebinding
  • calling "bind" on the same parameter twice (i.e. the very first time, execute, then bind again for the subsequent call) sets the class into an internal state which says "if they don't bind all the parameters before the next execute, throw an exception".

I'll be honest and say that the third feels like hidden behaviour. 1 and 2 probably cover us off. We have to consider that a programmer may want to re-use parameters from a previous execution, so even enforcing 1 might be cumbersome.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions