Is there a way to bind the array with mysqli preparation

I am trying to create a class that will execute any of several stored procedures with any number of variables

Im using php and mysqli

  • My class enumerates an array and builds a string based on the number of elements if any
  • giving something like this CALL spTestLogin(?,?)

    eg
  • Now I need to bind an input from my array using something like this:

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

Then if it works I can work on getting the results

Any ideas

+1


source to share


2 answers


You need to do something like this:

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

      



given which $this->paramTypes

is a string in the format required mysqli_stmt::bind_param

- if not, you must create this parameter first string

.

I don't know if the parameters work in this case out

or inout

.

+3


source


mysqli_stmt::bind_param()

will take a variable number of arguments

Assuming it is $this->paramTypes

also an array containing the correct type character for each variable (one of "i", "d", "s", "b"), you can 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 parameters that you would normally pass to bind_param () and then make the call with call_user_func_array()

There might be a much better way to do it

Edit : just realized that I was beaten at the time of writing this, I'll leave this answer here for the moment in case it's interesting

+2


source







All Articles