Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with "forgotten binds" #12

Open
adrianhardy opened this issue Jan 31, 2014 · 0 comments
Open

Deal with "forgotten binds" #12

adrianhardy opened this issue Jan 31, 2014 · 0 comments

Comments

@adrianhardy
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant