Is there a way to bind an array to mysqli prepare [duplicate]


I'm trying to make a class that will execute any one of a number of stored procedures with any amount of variables

Im using php and mysqli

  • My class enumerates an array and constructs a string based on the number of elements if any
  • giving something like this CALL spTestLogin(?,?) for example
  • I now need to bind the input from my array using somethin like this:

    $stmt->bind_param($this->paramTypes,$this->paramValues);//paramValues is my array

Then if that works I can work on getting my results

Any ideas


You have to do something like this:

call_user_func_array(array($stmt, 'bind_param'), $params);

given that $this->paramTypes is a string in the format required by mysqli_stmt::bind_param - if not, you have to create this string parameter first.

I don't know if out or inout parameters do work in this case.

mysqli_stmt::bind_param() will take a variable number of arguments

Assuming $this->paramTypes is also an array holding the correct type character for each variable (one of 'i', 'd', 's', 'b'), you could do something like

$params = $this->paramValues;
array_unshift($params, implode($this->paramTypes));
call_user_func_array( array( $stmt, 'bind_param' ), $params);

Essentially you create an array of the parameters you would normally pass to bind_param(), and then make the call using call_user_func_array()

There may be a much better way of doing this

Edit: just realised I was beaten to it while writing this, I'll leave this answer here for now in case it is of interest

By : Tom Haigh

This page explains why you get an array, and the meaning of the numbers in it, at least.

By : unwind

This video can help you solving your question :)
By: admin