tags:

views:

180

answers:

1

Hello,

Please see below my code. I am attempting to bind an array of paramenters to my prepared statement. I've been looking around on the web and can see I have to use call_user_func_array but cannot get it to work. The error I get is: "First argument is expected to be a valid callback, 'Array' was given" I may be wrong but I'm assuming the first argument can be an an array and perhaps this error message is misleading. I think the issue is that my array is in someway at fault. Can anyone see what I am doing wrong? Thanks.

$type = array("s", "s");
$param = array("string1","anotherstring");

$stmt = $SQLConnection->prepare("INSERT INTO mytable (comp, addl) VALUES (?,?)");

$params = array_merge($type, $param);

call_user_func_array(array(&$stmt, 'bind_param'), $params);
$SQLConnection->execute();
+1  A: 

I wouldn't know why you have to use call_user_func_array, but that's another story.

The only thing that could be wrong in my eyes is that you are using a reference to the object. Assuming you're using PHP 5.*, that is not necessary:

call_user_func_array(array($stmt, 'bind_param'), $params);
Franz
Thanks Franz, I'm using the call_user_func_array because the number of parameters to bind varies. I am using PHP5 but removing the reference staill produces the same result.
Columbo
Then that must mean something is wrong with your object. See this PHP "bug" report: http://bugs.php.net/bug.php?id=46229. What does `var_dump($stmt)` give you at that place?
Franz
Yes, I'm just getting bool(true) rather than the statement.Not sure why going to have a look. I suppose it's giving me a TRUe to say the statement preperation was a success. But I've seen people doing what I'm doing above in several examples.
Columbo
You were right it was an object issue. I used stmt_init when I didn't need to to create $SQLConnection. I was still expecting an object out of that but removing it did the trick. Thanks.
Columbo