0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Read the PHP documentation of [references](https://www.php.net/manual/en/language.references.php) – Barmar Jan 05 '20 at 18:25
  • If you are still using PHP 5 I strongly recommend to upgrade as soon as possible. This version is no longer supported. [Let Rasmus Lerdorf explain it to you](https://youtu.be/wCZ5TJCBWMg?t=2434) – Dharman Jan 05 '20 at 21:06
  • Did you see the other answer in the thread you linked? This one: https://stackoverflow.com/a/50682298/1839439 – Dharman Jan 05 '20 at 21:09
  • @Barmar i knew &$var in order to change $var variable if it's called by a function. But i don't undertand why this was happened. Because i did "echo" both of $bind_params[] in those two trials, those returned the same array elements. – 01_Secret.Lubis_10 Jan 06 '20 at 04:03
  • @Dharman Thanks for your suggesting Brother. But unfortunately my company still want to use this php version, :( – 01_Secret.Lubis_10 Jan 06 '20 at 04:05

1 Answers1

-1

The & reference to a variable, this means it gives the address in the memory to the function() and doesn't copy the value into a new part of the memory. How it works look here: Passing by Reference -> PHP-Docu and as @Brama toled, read how References work

You can find descriptions with examples to the function in the PHP-Documentation call_user_func_array ()

It returns the return value of the callback or FALSE on 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

The warning means the expected parameter can not be processed, for this reason, the Fatal error, because fetch_assoc() expects a correct DB-result

You can prevent this with:

if ($result = $mysqli->query($query)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
    printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}

/* free result set */
$result->free();
}

source: PHP-Manual

M. Rostami
  • 999
  • 3
  • 16
  • 27
  • 1
    Please read [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jan 05 '20 at 21:07
  • @Janick Koder I've tried your suggested code sir. But the warning still come up. ` 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 552 ` Mybe i should use reference (&$bind_params). Shouldn't i brother ? – 01_Secret.Lubis_10 Jan 06 '20 at 10:06
  • @Dharman :Thanks for your suggested reference link. It would be worthy. – 01_Secret.Lubis_10 Jan 06 '20 at 10:09