r/ProgrammerTIL • u/ghedipunk • Nov 09 '18
PHP TIL that references accessing array elements do not need that element to exist at first, EXCEPT in PDO prepared statement bound parameter, but only when accessing that element in a foreach.
Just accessing a elements in scalars and arrays (and I assume public object properties) are straightforward:
Based on this, one would expect ALL of these to insert 0 through 9 into a database.
However, the last set ($sixthStatement, the foreach on a nested array) does not. It inserts NULLs.
<?php
// Insert $dbname, $host, $username, and $password here. ;-)
$connection = new PDO('mysql:dbname=' . $dbName . ';host=' . $host, $username, $password);
/**
* Table creation query:
* CREATE TABLE `test` (
* `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
* `value` INT(10) UNSIGNED NULL DEFAULT '0',
* PRIMARY KEY (`id`)
* )
*/
$query = "INSERT INTO test (value) VALUES (:value)";
$firstStatement = $connection->prepare($query);
$secondStatement = $connection->prepare($query);
$thirdStatement = $connection->prepare($query);
$fourthStatement = $connection->prepare($query);
$fifthStatement = $connection->prepare($query);
$sixthStatement = $connection->prepare($query);
$a = 0;
$b = array();
$c = array(0 => 0);
// $d intentionally not set.
$e = [0,1,2,3,4,5,6,7,8,9,];
$f = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],];
// ::bindParam gets the parameter by reference.
$firstStatement->bindParam(':value', $a); // The pre-set scalar.
$secondStatement->bindParam(':value', $b[0]); // The array without any elements.
$thirdStatement->bindParam(':value', $c[0]); // The array with an element assigned.
$fourthStatement->bindParam(':value', $d); // An unset variable, to be used as a scalar.
$fifthStatement->bindParam(':value', $eValue); // For use in a foreach, accessing a value.
$sixthStatement->bindParam(':value', $fValue[0]); // For use in a foreach, accessing an element.
for ($i = 0; $i < 10; $i++) {
$a = $i;
$firstStatement->execute();
}
for ($i = 0; $i < 10; $i++) {
$b[0] = $i;
$secondStatement->execute();
}
for ($i = 0; $i < 10; $i++) {
$c[0] = $i;
$thirdStatement->execute();
}
for ($i = 0; $i < 10; $i++) {
$d = $i;
$fourthStatement->execute();
}
foreach ($e as $eValue) {
$fifthStatement->execute();
}
foreach ($f as $fValue) {
$sixthStatement->execute();
}Implications are, of course, don't run queries inside of loops, which we should all know by now due to the performance implications of querying a DB in a loop... but now there's an extra reason to be wary: PDOStatement::bindParam() isn't consistent.
View all comments
Show parent comment
4
u/ghedipunk Nov 09 '18
~double checks the description of /r/programminghorror~
Are you saying that this is a terrifying coding story or that this code is bad?
Because I really don't see any stories here, and yes, the code I wrote to illustrate this is REALLY bad.