php - MySQLi "bind_param" in cycle -
why can not use "bind_param" this? there alternative ways use binding in cycle?
$insert = $this->db->prepare('insert '.$tablename.' ('.implode($colum, ', ').') values ('.implode($placeholder, ', ').'); '); ($i=0;$i<$count;$i++) { $insert->bind_param($query[$i]['type'], $query[$i]['value']); }
well, error makes pretty clear: $placeholder
array doesn't contain same number of placeholders have parameters in $query
array.
check code building $placeholder
, $query
arrays. if can't find problem, add piece of code in question.
ok, sorry, i'm not used mysqli
. apparently have pass parameters in 1 call bind_param
. that's annoying, there's workaround.
the call_user_func_array
function allows pass arguments function array.
so can:
- construct string of types looping through parameters;
- make array
$params
string @ index 0, , parameters' values @ subsequent indexes; - call
call_user_func_array(array($insert, 'bind_param'), $params);
.
that this:
$insert = $this->db->prepare('insert '.$tablename.' ('.implode($colum, ', ').') values ('.implode($placeholder, ', ').'); '); $values = array(); ($i=0 ; $i<$count ; $i++) { $types .= $query[$i]['type']; // needs 1 single character [idsb] $values[] = $query[$i]['value']; } $params = array_merge(array($types), $values); call_user_func_array(array($insert, 'bind_param'), $params);
Comments
Post a Comment