r/ProgrammerTIL • • 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:

https://3v4l.org/QFbtT

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.

0 Upvotes

12 comments sorted by

View all comments

Show parent comment

1

u/ghedipunk Nov 10 '18

You seem to have missed the 3v4l link at the top of the post, that demonstrates exactly what I'm doing with the reference passed to bindParam().

The important area starts at line 43.

Read it carefully. If your reasoning is correct, then the output would be all nulls there, as well.

2

u/wvenable Nov 10 '18 edited Nov 10 '18

Nope. In your link, you're taking a reference to the whole variable and not an single element like are you with the bindParam. That's the whole difference.

Here's an example: https://3v4l.org/sDFmS

1

u/ghedipunk Nov 10 '18

Great catch. Thank you.