title: How to call mysqli_stmt with call_user_func_array?
body: <p>I am trying to pass a variable number of arguments to a method of a class. Here is the function of the class that I am trying to invoke:</p>
  
  <pre><code>class DbHelper{
  ....
      public function Execute($query, $params){
          $this-&gt;open();
          $stmt = $this-&gt;mysqli-&gt;prepare($query) or die($this-&gt;mysqli-&gt;error);
          call_user_func_array(array($stmt, 'bind_param'), $params);  // 1
          return $stmt-&gt;execute() ? $stmt-&gt;num_rows : 'ERROR';
      }
  ....
  }
  </code></pre>
  
  <p>Here is the code I am using to invoke the function:</p>
  
  <pre><code>....
  $conn = new DbHelper();
  $params = array('ss', $ID, $i);
  $conn-&gt;Execute('INSERT INTO some_table (ID, `Index`) VALUES (?,?)', $params);
  ....
  </code></pre>
  
  <p>This gives an error:</p>
  
  <blockquote>
    <p>Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ..mypath..\dbhelper.php on line </p>
  </blockquote>
  
  <p>In my another post (click <a href="https://stackoverflow.com/questions/12003890/cannot-use-call-user-func-array-on-mysqli-stmt-object">here</a>) on same issue few months back, some on suggested me to use the code below:</p>
  
  <pre><code>call_user_func_array(array($stmt, 'bind_param'), refValues($params));
  
  function refValues($arr){
      if (strnatcmp(phpversion(),'5.3') &gt;= 0){ //Reference is required for PHP 5.3+
          $refs = array();
          foreach($arr as $key =&gt; $value)
              $refs[$key] = &amp;$arr[$key];
          return $refs;
      }
      return $arr;
  }
  </code></pre>
  
  <p>Using this again gives the same error. If I invoke the function like this (adding &amp; before $params):</p>
  
  <pre><code>$conn-&gt;Execute('INSERT INTO some_table (ID, `Index`) VALUES (?,?)', &amp;$params); // 2
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>Deprecated: Call-time pass-by-reference has been deprecated in ..mypath..\save.php on line </p>
  </blockquote>
  
  <p>Alternatively, I have tried this definition for function <code>DbHelper::Execute</code>:</p>
  
  <pre><code>public function Execute($query, $params){
      $this-&gt;open();
      $stmt = $this-&gt;mysqli-&gt;prepare($query) or die($this-&gt;mysqli-&gt;error);
      $stmt-&gt;{'bind_param'}($params); // 3
      return $stmt-&gt;execute() ? $stmt-&gt;num_rows : 'ERROR';
  }
  </code></pre>
  
  <p>Invoking this shows the error as: </p>
  
  <blockquote>
    <p>Warning: Wrong parameter count for mysqli_stmt::bind_param() in
    ..mypath..\dbhelper.php on line </p>
  </blockquote>
  
  <p>Where as, the parameter count is correct to bind_param, if the function is called properly.</p>
  
  <p>The version of PHP is 5.3.8.</p>
  
  <p>Any help please?</p>