I create a class that hold mysqli
object link with a private function to generate params that needed by bind_params
function which then will be used by this function:
call_user_func_array(array($stmt, 'bind_param'), $params);
Since, bind_param
need to pass value by reference, so I create a the function like this:
// $values is taken from $_POST's values
private function ¶ms($values) {
// Generate first value for bind_params like: 'ssis'
$param_type = '';
foreach ($values as $value) {
$param_type .= gettype($value)[0];
}
// Generate the rest value for bind_params
$params[] = & $param_type;
for ($i = 0; $i < count($values); $i++) {
$params[] = & $values[$i];
}
// The returned values should be like { 'ssi', $str_value, $str_value, $int_value }
return $params;
}
How I execute prepared statement is like this:
function insert_prepared($query, $posted) {
$params = &$this->params(array_values($posted));
$stmt = $this->mysqli->prepare($query);
if ($stmt) {
call_user_func_array(array($stmt, 'bind_param'), $params);
if ($stmt->execute()) {
return true;
} else {
return false;
}
}
}
Okay, now the problem is why call_user_func_array(array($stmt, 'bind_param'), $params)
still complaints about expected to be a reference?
Did the manual said the function should be written like function ¶ms($values)
?
What did I missing?