Prepared statement errors with PHP 5.3 and mysqli. call_user_func_array returns null. Type error?
Prepared statements using mysqli seems to be having issues with PHP 5.3. I kept getting null from a prepared statement command that worked fine in PHP 5.2, namely, the mysqli_stmt_bind_param
Null means that the array you passed didn’t match the function signature.
The following worked in php 5.2, but when I updated my Mac to Snow Leopard (PHP 5.3 included) the following function stopped working
call_user_func_array(’mysqli_stmt_bind_param’, array($stmt,’isis’,$args));
I kept getting a null return value (note – different than false). See bottom of the following bug: http://bugs.php.net/bug.php?id=47554&edit=1
"Returns the function result, NULL for an invalid callback, and FALSE for all other errors."
Toying with the array (translates to method signature of the callback function) passed to call_user_func_array was key. After much head banging (on desk) and much head banging (to music) I found that the following made my one specific test query work:
call_user_func_array(’mysqli_stmt_bind_param’, array($stmt,’isis’,(int) $args[2],(string) $args[3],(int) $args[4],(string) $args[5]));
I wonder if the library that mysqli uses is in c (so that mysqli_stmt_bind_param is eventually run at a level where typing needs to be determined), and is requiring typed values that the php is screwing up somewhere in the translation. Or on the mysqli level. And I wonder what other explanations there could be for this behavior.
Usually when code is this buggy it is a developer’s coding errors, but someday I hope to run into a bug that isn’t my fault. On second thought, finding a bug in your development language probably causes a lot more pain in trying to figure it out, so I’m not sure that would be much of an accomplishment.

Hi,
Great post, I recently ran into this same bug, while trying to make a call to mysqli_stmt_bind_param using call_user_func_array. Have you found any solution that will still allow to bind a variable number of parameters?
Thanks,
Emil
Unfortunately I never found a work-around to use a variable number of params. Which is still frustrating considering mysqli is the improved mysql library, and 5.3 is the later and improved version of php. You would think more people would have this issue. I wonder if it is a variable length function problem. Maybe you can’t use call_user_func_array for functions with varable length parameters as a result of a bug or some misunderstanding on my part.
Let me know if you find the solution so I can get link to it and get an unresolved problem off the google search results.
I ended up recompiling php for snow leopard to be 5.2.8, since I had to recompile it anyways for an image library. And at work we switched to ruby on rails for a backend, so I don’t think I’ll be solving this one.
got a workaround. all bindings MUST be references to some other thing. i solve this like so:
//$arr = array_merge(array(&$statement, &$fmt), &$args); // stopped working in 5.3
$arr = array();
$arr[] = $statement;
$arr[] = $fmt;
foreach($args as $k => $v)
$arr[] = &$args[$k]; // *ref* is REQUIRED; array_merge doesn’t do that
me again. it seems to be a general PHP 5.3 issue: http://bugs.php.net/43568
Very nice! Thanks Chris. I remember running into reference issues in later php versions, things where functions required arguments that were by reference, and any fresh or deep-copy objects would show warnings or errors. Something like that. It is annoying how the ref usage is changing a bit. But for the sake of progress in php I can live with it.
Oh man, I just looked at the bug. Spot on. Nice find! Using the zend framework is exactly where I ran into these errors.
Add A Comment