Bind Param with array of parameters
The easiest way would be apparently switching from mysqli to PDO
It will let you to do it the way you want, and even without any additional functions:
function registerUser($firstName, $lastName, $address, $postcode, $email, $password)
{
$sql = "INSERT INTO Users VALUES (NULL, ?, ?, ?, ?, ?, ?)";
$stmt = $this->db->prepare($sql);
$stmt->execute(func_get_args());
}
call_user_func_array "Call a callback with an array of parameters"
call_user_func_array(array($stmt, "bind_param"), array_merge(array($type), $params));
should do the job
UPDATE: you have also to change your params array:
$params = array(&$firstName, &$lastName, &$address, &$postcode, &$email, &$password);
as mysqli_stmt::bind_param
expects the second and the following parameters by reference.
EDIT: Your query seems to be wrong. Maybe you have less fields than you have variables there. Do:
"INSERT INTO Users (field1, field2, field3, field4, field5, field6) VALUES (?, ?, ?, ?, ?, ?)"
where you replace the name of the fields by the correct names
As of PHP 5.6 you can utilize argument unpacking as an alternative to call_user_func_array, and is often 3 to 4 times faster.
<?php
function foo ($a, $b) {
return $a + $b;
}
$func = 'foo';
$values = array(1, 2);
call_user_func_array($func, $values);
//returns 3
$func(...$values);
//returns 3
?>
Taken from here.
So your code should look something like this:
public function bind($query, $type, $params)
{
$this->query = $query;
$stmt = $this->mysqli->prepare($this->query);
$stmt->bind_param($type, ...$params);
$stmt->execute;
}
You get your error "Call to a member function bind_param() on a non-object" most likely, because your $this->mysqli->prepare encounters some kind of error. (see http://php.net/manual/de/mysqli.prepare.php - it returns FALSE on error, which seems to be the case here)
After you have resolved that problem, try this instead of your call of $stmt->bind_param:
call_user_func_array(array($stmt, 'bind_param'), array_merge($type, $params));