mysqli_statement::bind_param($types, &$var1)
accepts variables by reference. So, it's intended to deal with variables that are not defined yet.
And normally you can do like this
$sql = $conn -> prepare("select * from table where id = ?");
$sql -> bind_param('i', $i);
$i = 1;
$sql -> execute();
and it would work flawless.
However, an array is another matter. Like it is noted my @mario in the comment, Referencing an undeclared variable automatically defines it. So, when you reference a variable (and here $sql->bind_param('i', $array[0]);
you are effectively doing it), both $array
and $array[0]
would be created. Where $array[0]
is a reference.
Were you assigning a value to this variable, i.e $array[0] = 1;
, it would have worked.
But you assigned an brand new value to the entire array. As a result, it now contains not a reference, but a new value.
The above can be illustrated with a simple code snippet
As you can see, as long as you retain the original array member, the reference remains.
But as soon as you assign a brand new value to the entire array, the reference is gone!
But it's gone only from the array, but not from the function. There it still points to that odd &NULL
value. That's why you cannot get any result.