I need to make a lot of mysql queries during data procession. As the actual code hardly changes, I would like to encapsulate it using a function. Such a function could be as follows:
// input: sql statement, binding parameters and paramter types
private prepared_statement($query, $params, $param_string) {
// I use this peace of code over and over again
$stmt = $this->db->prepare($query);
if (!$stmt) {//TODO log
echo "Error in prepared statement";
}
// problem
$stmt->bind_param($param_string, $param1, $param2, ...);
$stmt->execute();
// problem
$stmt->bind_result($);
$stmt->fetch();
$stmt->close();
//...
}
The second function argument is an array that contains all binding parameters of the query to be made. I do not know how to pass them to the bind_param function, because it only accepts the binding parameters as single variables.
Is it possible to convert dynamically the param array to variables which then can be passed to bind_param?
Furthermore I am wondering how to bind the results best, as I don't know how many results are going to be returned. Actually it is the same problem as above.
bind_paramin a loop, once for each variable (with$param_string[i], $param[i]as parameters for example...) ?