3

I have been trying to bind an array with a prepared statement for days, I have managed to create the number of ?,? and the i,i these are represented in my code as $params and $type. The problem comes when using call_user_func_array with my code since I haven't managed to make it work, I have tried multiple answers from this site but I haven't got it working.

The full query code is:

$params = implode(',', array_fill(0, count($greaterThan), '?'));
$stmt11 = $mysqli->prepare("SELECT nombre FROM usuarios WHERE id IN (".$params.")");
$type = implode('', array_fill(0, count($greaterThan), 'i'));
$param = implode(",", $greaterThan);
call_user_func_array(array($stmt11, "bind_param"), array_merge(array($type), $greaterThan));
print_r(error_get_last());
$stmt11->execute();
$stmt11->store_result();
$stmt11->bind_result($nombresmayores);
$arraynombresmayores = array();
$stmt11->store_result();
while($stmt11->fetch()){
    $arraynombresmayores[] = $nombresmayores;
}

Where $param are the values separated by comas (just in case you need it). The array I'm trying to bind is $greaterThan, the query works perfectly because I have made some debugging. The error the program outputs is:

Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given

Finally, the content of the array is:

array(2) {
  [0]=>
  int(2)
  [1]=>
  int(4)
}
C.Xammar
  • 167
  • 1
  • 1
  • 11

3 Answers3

1

if your php version is not outdated you can make it much simper

$params = implode(',', array_fill(0, count($greaterThan), '?'));
$stmt = $mysqli->prepare("SELECT nombre FROM usuarios WHERE id IN ($params)");
$types = str_repeat('i',count($greaterThan));
$stmt->bind_param($types, ...$greaterThan);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($nombresmayores);
$arraynombresmayores = array();
$stmt->store_result();
while($stmt->fetch()){
    $arraynombresmayores[] = $nombresmayores;
}

but better yet use PDO:

$params = implode(',', array_fill(0, count($greaterThan), '?'));
$stmt = $pdo->prepare("SELECT nombre FROM usuarios WHERE id IN ($params)");
$stmt->execute($greaterThan);
$arraynombresmayores = $stmt->fetchAll(PDO::FETCH_COLUMN);

just compare this neat code with that awful mess you have now.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

I have method i use in an class i made to work mysqli in a fashinable way. The key reside in the foreach loop: Basicaly you create a copy of your array with another one by reference and then bind the referenced array. Hope this helps.

UPDATED : in your case you need to create an array with your string $type and the array of greateThan. From the temp to the bind array, make sure the keys stay the sames and don't unset your data before having called the bond_param

$type = implode('', array_fill(0, count($greaterThan), 'i'));
//$param = implode(",", $greaterThan); //Not needed

//merge the 'type' as an array and the variables to pass
$temp_params= array_merge([$type],$greaterThan);

//Create a referenced array but dont'reference the 'type' argument
foreach($temp_params as $k=>$v){
  if($k===0){
    $bind_params[$k]=$temp_params[$k];
  }else{
    $bind_params[$k]=&$temp_params[$k];

  }
}

//use the referenced array 
call_user_func_array(array($stmt11, "bind_param"), array_merge(array($type), $bind_params));
Louis Loudog Trottier
  • 1,367
  • 13
  • 26
-1

Just try to assign that expression to a variable, and bind a variable. Expressions cannot be used as references, so you have to pass a variable.

Mateusz Sip
  • 1,280
  • 1
  • 11
  • 11