user13432
user13432

Reputation: 11

Efficient Loop to Bind Parameters in PHP

I am trying to bind an unknown number of parameters to a prepared PHP/MySQL statement. I tried the code below. However, it only works with queries with a set number of parameters and I know that there has to be a more efficient way to do so.

switch($numparams){
                    case 0:
                        @$stmt->bind_param("");
                        break;
                    case 1:
                        @$stmt->bind_param($paramtypes, $param_arr0);
                        break;
                    case 2:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1);
                        break;
                    case 3:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2);
                        break;
                    case 4:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3);
                        break;
                    case 5:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4);
                        break;
                    case 6:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5);
                        break;
                    case 7:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6);
                        break;
                    case 8:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7);
                        break;
                    case 9:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7,$param_arr8);
                        break;
                    case 10:
                        @$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7,$param_arr8,$param9);
                        break;
}

As you can see, it's completely inefficient to code and isn't scalable. I tried dynamically naming variables in a for-loop based on the number of parameters, but I couldn't figure out how to then insert them into the bind_param without having a separate bind_param statement for each in a switch-case loop (as above). It might just be that there is no way to do this, but I feel there should be one. If either are the case, I appreciate any help.

Upvotes: 1

Views: 45

Answers (1)

H. Sodi
H. Sodi

Reputation: 550

I think your solution lies in call_user_func_array() array.

First you need a string similar to "ssi...." that is the types of parameters in order.

Code

 $types = '';
    foreach($param_arr as $param) {
        $types.= substr(strtolower(gettype($param)), 0, 1);
    }
 call_user_func_array(array($stmt, 'bind_param'), array_merge(array($types), $param_arr)));

Reference

http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli http://php.net/manual/en/function.call-user-func-array.php

Hope this helps you through ;)

Upvotes: 1

Related Questions