1

I have PHP version 5.5.9-1 and having trouble with call_user_func_array and bind_param for stmt. I have the following code

$query = "SELECT * FROM studentTable WHERE firstname = ?";
if(!($stmt = $conn->prepare($query))){
    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
    return;
}
$paramType= 's';
$paramValue= 'bob';
$a_params = array(&$paramType, &$paramValue);
call_user_func_array(array($stmt, 'bind_param'),$a_params)

I am getting the following error and I don't know why

Binding parameters failed: (0) 

I have tried wrapping the strings "'s'" but that reports that there is not enough arguments. What am i doing wrong?

Thank you

UPDATE

I have tried the code from php5.3 - mysqli_stmt:bind_params with call_user_func_array warnings but still not working

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
DanielJ
  • 245
  • 1
  • 2
  • 10
  • why are you using call_user_func_array instead of calling bind_param on the the stmt directly? – Gordon Oct 17 '17 at 11:31
  • I want to dynamically add bindings, just trying to get example working – DanielJ Oct 17 '17 at 11:32
  • 1
    [Your code basically works](https://3v4l.org/EHOk2), are you 100% sure the error does not come from somewhere else ? Might it be possible, your error is not even an error? The way you check for an error is wrong I guess, because you don't check typesafe. – Daniel W. Oct 17 '17 at 11:48
  • 1
    This code lacks a verifiable example and a reliable error message as well – Your Common Sense Oct 17 '17 at 12:16

1 Answers1

-1

I had a similar problem (I just don't remember exactly what problem it was), I've solved this situation using ReflectionMethod.

$type = 's';
$parameter = 'bob';

$values = [$type , &$parameter];

$reflectionMethod = new \ReflectionMethod($statement , 'bind_param');
$reflectionMethod->invokeArgs($statement , $values);

$statement->execute();

The code above comes from a simple study case of my own. Created with PHP 5.5 and tested with PHP 7 (a long time ago...) on Debian Jessie.

After all, your code should work as well. With reflection, this code does the same thing as your code.

Ps.: as you can read in comments below, it slower than call_user_func_array and should be avoided in production.

Gabriel Heming
  • 1,100
  • 10
  • 30