php - MySQLi bind_param() reference -


i trying pass several parameters dynamically bind_param() function.

here error receive:

warning: parameter 2 mysqli_stmt::bind_param() expected reference, value given

code:

$con = new mysqli('localhost',user,pass,dbs);  if(mysqli_connect_errno()) { error(mysqli_connect_errno()); }  $con -> set_charset("utf8");  /*inside*/  $type='';  $query='select bugid bug';  if(!empty($_get['cena'])) {      $build[]='ucena=?';      $type.='i';     $val[]=$_get['cena'];  }  if(!empty($_get['popust'])) {      $build[]='upopust=?';      $type.='i';     $val[]=$_get['popust'];  }  if(!empty($build)) {      echo $query .= ' '.implode(' , ',$build); }  $new = array_merge(array($type),$val); foreach($new $key => $value) {     $tmp[$key]=&$new[$key];      }  echo '<br/><br/>';  foreach ($new $new ){      echo "$new<br/>"; }  if ($count = $con->prepare($query)) {      call_user_func_array(array($count,'bind_param'),$tmp);     $count->execute();     $cres = $count->fetch_row();      $count -> close();  } else error($con->error);   /*inside*/  $con -> close(); 

i'm sorry this, code terrible. unreadable , hard maintain in production environment.

one example: using these lines:

foreach($new $key => $value) {     $tmp[$key]=&$new[$key]; } 

you have used:

foreach($new $key => $value) {     $tmp[$key]= $value; } 

which have displayed understanding of foreach statement. also, using more descriptive variable names $tmp , $new improve readability of code a lot. there many more issues code, let's focus on issue.

the major problems in line:

if ($count = $con->prepare($query)) { 

and line:

call_user_func_array(array($count,'bind_param'),$tmp); 

mysqli::prepare() returns mysqli_statement (as described here), not sort of count. try using $count = count($tmp) instead, if need determine amount of parameters.

the error you're seeing result of usage of call_user_func_array(). described on php.net page on bind_param:

care must taken when using mysqli_stmt_bind_param() in conjunction call_user_func_array(). note mysqli_stmt_bind_param() requires parameters passed reference, whereas call_user_func_array() can accept parameter list of variables can represent references or values.

the best solution provided in comments on same page:

coming problem calling mysqli::bind_param() dynamic number of arguments via call_user_func_array() php version 5.3+, there's workaround besides using function build references array-elements. can use reflection call mysqli::bind_param(). when using php 5.3+ saves 20-40% speed compared passing array own reference-builder-function.

example:

<?php  $db     = new mysqli("localhost","root","","tests");  $res    = $db->prepare("insert test set foo=?,bar=?");  $refarr = array("si","hello",42);  $ref    = new reflectionclass('mysqli_stmt');  $method = $ref->getmethod("bind_param");  $method->invokeargs($res,$refarr);  $res->execute();   ?> 

hope helps.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -