0

I wanna add dynamically variables to mysqli:bind_param ...

I saw a lot of tutorials and examples but the code I made isn't working for hours now.

$params = array('');
$params[0] = $variableType; //variableType contains e.g. sssi

foreach ( $data as $key => $value ) {
    array_push($params, $data[$key]); // I also tried $params[] = $value;
}

call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));

The refValues function looks like this ...

private function refValues(Array &$arr)
{
    $refs = array();
    foreach($arr as $key => $value)
        $refs[$key] = &$arr[$key];
    return $refs;
}

Error given every time I try it ...

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in C:\Users...xxx.php on line 54

Ilario Engler
  • 2,419
  • 2
  • 27
  • 40
  • ok, but I don't see the code where you are calling bind_param yet – Gabriel Rodriguez Sep 22 '15 at 00:25
  • 1
    Have you considered using PDO instead? This would not even be an issue... – AcidReign Sep 22 '15 at 00:29
  • If `$params` is an Array already `$this->refValues` is pointless. `call_user_function_array` is calling the `bind_param` method of `$stmt` which should be based on `$stmt = mysqli_connection->prepare`. Maybe there is a problem with `$data`. – StackSlave Sep 22 '15 at 00:45

1 Answers1

-1

The problem here is that you are missing the first parameter to mysqli_stmt::bind_param, from: http://php.net/manual/en/mysqli-stmt.bind-param.php

bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )

So you need to assemble the $types string and pass it as the first parameter.

EDIT I suggest that instead of trying force mysqli to do something its not design for, you should use PDO which simply takes an array of parameters in the execute method: http://php.net/pdostatement-execute

AcidReign
  • 504
  • 3
  • 6