0

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.

8
  • can't you call bind_param in a loop, once for each variable (with $param_string[i], $param[i] as parameters for example...) ? Commented Sep 1, 2015 at 13:36
  • Are you talking something like variable length args? Commented Sep 1, 2015 at 13:40
  • Or potentially something more like call_user_func_array? Commented Sep 1, 2015 at 13:42
  • You can only loop over bind_param if you execute the statement in each iteration. This would cause a high overhead. Commented Sep 1, 2015 at 13:42
  • Jon, call_user_func_array seems tempting, but I guess you cannot pass a fixed string as the first paramter and convert the array to the 2nd, 3th,... parameter?! Commented Sep 1, 2015 at 13:47

3 Answers 3

1

If you have a array like this:

$array = Array("name"=>"Jhon","surname"=>"Conor");

you can do:

extract($array);

and then use like this:

echo "Your name is " . $name . " and your surname is " . $surname;
Sign up to request clarification or add additional context in comments.

1 Comment

This approach only works if I knew how many binding parameters exist. As each query look differently I cannot foresee the size of '$array'.
1

With the help of Jon's proposal to use call_user_func_array I managed to get the following solution:

private function prepared_statement($query, $params, $param_string) {

        $fetched_data = array();
        $binding_params[] = & $param_string;

        foreach($params as &$current_param) {
            $binding_params[] = & $current_param;
        }

        $stmt = $this->db->prepare($query);
        if (!$stmt) {
            //TODO log
            echo "Error in prepared statement";
        }

        call_user_func_array(array($stmt, "bind_param"), $binding_params);
        $stmt->execute();   
        $result = $stmt->get_result();
        while($row = $result->fetch_array(MYSQLI_ASSOC)) {
            foreach($row as $r) 
                array_push($fetched_data, $r);

        }

        $stmt->close(); 
        return $fetched_data;
    }

Comments

0

You can use the '$' twice to get the variable:

foreach($my_arr AS $k => $v){
    $$k = $v;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.