I have the following
- A multidimensional array encoded as JSON. Each index holds exactly 80 keys
- A CSV file with 80 items representing identical keys in the indexes above since dumping them in the same script will be too cumbersome
First, I mutate the original keys in the JSON array to use underscores since named placeholders don’t accept that
foreach ($content as $key => &$bulk) {
$vals = array_values($bulk);
$keys = array_map(function ($item) {
$item = str_replace(' ', '_', $item);
return ":$item"; // semi-colons are redundant but I tried it also to be on the safe side
}, array_keys($bulk));
$bulk = array_combine($keys, $vals);
// add date to each fixture
$bulk[':date'] = date('Y-m-d');
}
Then I do the exact same thing for items in the CSV file and convert them to what can be used in the actual query
// swap out spaces
array_walk($validColumns, function (&$val) {
$val = str_replace(' ', '_', $val);
});
// prepare named placeholders
$transform = array_map(function ($val) {
return ":$val";
}, $validColumns);
$placeholders = implode(',', $transform);
$validColumns = array_map(function ($val) {
return "`$val`";
}, $validColumns);
After this, I build the query using the following string
$query = 'INSERT INTO container (' . implode(',', $validColumns) . ") VALUES ($placeholders)";
And attempt to execute with this
try {
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$transaction = $conn->prepare($query);
foreach ($content as $key => $bulk) {
if (!is_array($bulk)) $bulk = [$bulk];
var_dump(array_diff($transform, array_keys($bulk))); // this returns an empty array even when I swap the arguments
$transaction->execute($bulk);
}
But it keeps throwing the dreaded error SQLSTATE[HY093]: Invalid parameter number: parameter was not defined’. My question is how may I know what keys I’m missing, from the nested array_diff in the foreach?
When I var_dump the lengths, they both return 80. Same with the placeholder values. I have also tried using the object keys without adding the semi-colon but the issue persists.
Are there any additional tools I can use to detect why it relentlessly throws the error as per what the inconsistency is–whether it can’t find a matching placeholder or if there are more values than placeholders? I have scoured every single question with this error down to the 16th search result and every time, someone posts his query and bound parameters but in my case where I have 80 length arrays and 80 length cryptic compound-word strings representing keys, that will be an effective way to deter anyone from trying to help.