So currently I got this code for preparing queries which works but its inefficient, and might require expansion if the queries become bigger. And there are more pieces of code like this. One example of these pieces of codes goes as follows.
//This function is called when preparing statements to prevent sql-injections(1)
//It binds the variables to the prepared statements based on an array.
//$stmt is the prepared statement. $params is an array with the input data.
public function bind($stmt, $params){
x = count($params);
$type = $this->getType($params); //simply obtains the types of the params(2)
if($x == 1){
$stmt->bind_param($type, $params[0]);
}elseif($x == 2){
stmt->bind_param($type, $params[0], $params[1]);
}elseif($x == 3){
$stmt->bind_param($type, $params[0], $params[1], $params[2]);
}else{
echo "Too much params"; //error if there are more than 3 params.
}
return $stmt;
}
Referenced links from code for extra information, not that it really matters since its just an example.
(1) = How can I prevent SQL injection in PHP?
(2) = http://www.php.net/manual/en/mysqli-stmt.bind-param.php
So as you can see this will return an error if $params contains more than 3 items. So I have been trying to fix this in a more efficient way, because adding more elseif statements is not efficient. I tried all kinds of things with loops, but no good or working results. This problem does not only occur for just this bind_param example, but to other functions as well.
So I would like an easy efficient solution to add an amount of variables to a function-call based on the amount of array items which can be applied to more than just this example.