I want to do myqsli_stmt_bind_param
using call_user_func_array()
, because I have dynamically values that should be passed to the query.
First, I tried just directly put my $bind_params[] = [$type, $value]
(without &) as second parameter of call_user_func_array()
.
And second try I put my $bind_params[] = [&$type, &$value]
as second parameter of call_user_func_array()
.
My first trial is this (without &):
// To store all collected type into $bind_params[].
$bind_params = array($bind_params_1);
$count = count($bind_params_2);
// To store the all values into $bind_params[].
// $bind_params_2[] is collected $_POST values.
for ($i=0; $i < $count; $i++) {
$bind_params[] = $bind_params_2[$i];
$i++;
}
// Doing $stmt->bind_params($bind_params_1, $bind_params_2).
call_user_func_array(array($stmt, "bind_param"), $bind_params);
$stmt->execute();
If I run above code, this will produce a warning and an error:
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in C:\xampp\htdocs\dashboard\x\xx\xxx\xxxx\xxxxx\gb_daftar_usulan.php on line 571
Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\dashboard\x\xx\xxx\xxxx\xxxxx\gb_daftar_usulan.php on line 584
And then my second trial look like this (with &) :
// To store all collected type into $bind_params[].
$bind_params = array(&$bind_params_1);
// $bind_params_2[] is collected $_POST values.
$count = count($bind_params_2);
// To store the all values into $bind_params[].
for ($i=0; $i < $count; $i++) {
$bind_params[] = &$bind_params_2[$i];
$i++;
}
// Doing $stmt->bind_params($bind_params_1, $bind_params_2).
call_user_func_array(array($stmt, "bind_param"), $bind_params);
$stmt->execute();
The second code produce the right one result for me. I found this approach from here : (https://stackoverflow.com/a/24713481).
Why was this happened ?
What do exactly &
in &$bind_params_1
and &$bind_params_2
mean?
Why my first trial gave a warning and an error?