ITSagar
ITSagar

Reputation: 703

binding parameters dynamically in mysqli prepared statement

I am trying to get user preferences from some filters and then generate a dynamic query with dynamic parameters depending on his choices. I have implemented the following code earlier also on a local server and it worked fine, but this time I am trying to make it work on a Remote web hosting server and it is not working.

function set_filters() {
 $paramtype=array();
 $paramvalues=array();
 $params=array();
 $myqry = "SELECT * FROM users WHERE ";
 $initflag=0;
 if(isset($_POST["memtype"]) && !($_POST["memtype"]=="selects"))
 {
  $myqry.= "acctype=? ";
  $initflag=1;
  $paramtype[]="s";
  $paramvalues[]=$_POST['memtype'];
 }
 if(isset($_POST["country"]) && !$_POST["country"]=="selects")
 {
    if($initflag == 0)  {
        $myqry.= "country=? ";    
    }
    else  {
        $myqry.= "AND country=? ";            
    }
    $initflag=1;
    $paramtype[]="s";
    $paramvalues[]=$_POST['country'];
 }
 $params[] = implode("",$paramtype);
 $params = array_merge($params, $paramvalues);    
 echo "<script>alert('" . $myqry . "')</script>";
 return array($myqry, $params);
}
$filterres=set_filters();
$qry=$filterres[0];
$param=$filterres[1];
var_dump($qry);
var_dump($param);
if($result1 = $con->prepare($qry)){
  call_user_func_array(array($result1, 'bind_param'), refValues($param));
  echo "Step Main: Now it should work";
  $result1->execute();
  $res = $result1->get_result();
  if($res->num_rows == 0) {
     echo "Cannot reach the server.";
     exit();
  } 
  else {
     echo $res->num_rows;

The result of var_dump($qry) is:

string(36) "SELECT * FROM users WHERE acctype=? "

The result of var_dump($param); is:

array(2) { [0]=> string(1) "s" [1]=> string(6) "Leader" }

On live server, i am not getting any errors or warnings. The code is not reaching the following statement:

echo "Step Main: Now it should work";

Where I am making a mistake or what is the issue here out? Please help.

Upvotes: 1

Views: 181

Answers (1)

Shobhit Gupta
Shobhit Gupta

Reputation: 690

on running this code on my local machine I found that refValues($param) gives error. It seems like this is a user-defined function and you may be missing its in your code. Check it again and let me know.

Upvotes: 1

Related Questions