-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
sqlite non-sequential keys in array throws error when passed to execute with PDO #16081
Comments
While counterintuitive at first glance, this seems like the correct behavior to me: executing with an array has PHP do the parameter binding, as in In other words, the array passed to How would you feel if this was more clearly documented? I see there is a user note about this from a while back, but I did have to scroll a ways before I found it: |
In reading the notes, everyone is saying that you can't use an associative array, it has to have numeric indexes so there should be no reason to put them out of order from the question mark placeholders. Comments from the notes: // Array must be keyed with integers starting from zero This is right from the notes $placeholders = implode(',', array_fill(0, count($key), '?')); I would simply expect and want execute to take the array items in the order they are in the array without regard to the numerical indexes mattering. What use case is there for [1=>"second", 0=>"first"]; That seems like very poor programming. I my case I was running a $array = array_unique($array); to remove any duplicates. Having to then run this: $array = array_values($array); seems like a lot of fuss to make it work right. It supports If PDO is expecting an numeric indexed array, could it not run the array though a function like this? #include <stdio.h> typedef struct { char** array_values(MapEntry* map, int size, int* outSize) { $array = array_values($array); |
Changing the behavior of how the parameters are bound would be a BC break. Certainly doable, but is it worth it? |
Maybe @SakiTakamachi has an opinion about this issue? Otherwise I think this needs the RFC process, or at least proper discussion on the internals mailing list. |
IMHO, I think the safest option is to leave it as is and update the documentation. BC breaks in the cases Christoph describes, and I believe that treating arrays as if they were doing array_values could lead to ambiguities that could result in unintended bugs. If you strongly prefer this change, you can of course discuss it on the internal mailing list :) |
There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests. |
Description
PHP 8.2
$dsn = 'sqlite:'.$config->database;
$this->obj = new PDO($dsn);
$this->obj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
The following code:
Resulted in this output:
But I expected this output instead:
The $stmt->execute($values) method should use a foreach instead of a for loop or run a:
$values = array_values($values); over the incoming array so people don't have to bang their head against a way trying to figure out why their query will not run.
PHP Version
PHP 8.2
Operating System
CloudLinux v9.4.0
The text was updated successfully, but these errors were encountered: