0

I don't know why, but this doesnt work.

array_unshift($params,$types);
if($stmt = $conn->prepare($sql)){
  call_user_func_array(array($stmt, 'bind_param'), refValues($params));

  $stmt->execute();

Now it says:

Warning: mysqli_stmt::bind_param() expects parameter 1 to be string, array given...

So what is wrong with this code?

function refValues($arr){
if(strnatcmp(phpversion(),'5.3') >= 0){ //Reference is required for PHP 5.3
    $refs = array();
    foreach($arr as $key => $value){
        $refs[$key] = &$arr[$key];
    }
    return $refs;
}
return $arr;
}

The print_r of refValues($params);

Array ( [0] => Array ( [0] => i [1] => i [2] => i [3] => i [4] => i [5] => i [6] => i [7] => i [8] => i [9] => s [10] => i [11] => i [12] => i [13] => i [14] => i [15] => i [16] => i [17] => i [18] => i [19] => i [20] => i [21] => i ) [1] => 1 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 1241 [9] => [10] => [11] => 1 [12] => [13] => [14] => [15] => 432 [16] => 243 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 78 ) 

Here's the solution: I just had to implode the $types array. So Then the first parameter is a string and not an array anymore.

10
  • and what does make the error is not clear enough? Commented Jun 20, 2017 at 11:20
  • @hassan Which one is "parameter 1"? Commented Jun 20, 2017 at 11:21
  • 1
    @pandaNine refValues($params). Commented Jun 20, 2017 at 11:22
  • There are 2 Arrays, is that correct? I'm not sure if this can work. Commented Jun 20, 2017 at 11:42
  • @modsfabio Is there any alternative? the array at array[0] contains all datatypes. Commented Jun 20, 2017 at 11:45

1 Answer 1

1

I don't know what's inside $params, but mysqli_stmt::bind_param expects the second and the following parameters by reference.

bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )

Source: Doc: mysqli_stmt::bind_param

So you have to change the array values to references. For example:

$params = array(&$var1, &$var2, &$var3);

In case your array is dynamic, use the following function to convert it:

function arrayToRef(&$rawArray)
{ 
    $refArray = array(); 
    foreach($rawArray as $key => $value) 
    {
        $refArray[$key] = &$rawArray[$key];
    }
    return $refArray; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

I want to bind the parameters dynamically. You solution doesn't work dynamically, doesn't it?
You can convert the array, I added it to my answer
The function I used does the same as yours do: function refValues($arr){ if(strnatcmp(phpversion(),'5.3') >= 0){ //Reference is required for PHP 5.3 $refs = array(); foreach($arr as $key => $value){ $refs[$key] = &$arr[$key]; } return $refs; } return $arr; } So the error message is still the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.