1

Why its not working? I am trying to dynamically bind params based on what user send GET data. Not sure what is wrong about it.

    $bind_param = array();
    $array_param = array();
    $params = array();
    $i = 0;
    $bind_param[$i] = "i";
    $array_param[$i] = $_GET['p2'];

    $query = "SELECT * from point where fixture_id = ? ";

    if(isset($_GET['p1'])){
        $query .= "and user_id = ? ";
        $i++;
        $bind_param[$i] = "i";
        $array_param[$i] = $_GET['p1'];
    }

    $param_type = '';
    $n = count($bind_param);
    for($i = 0; $i < $n; $i++) {
      $param_type .= $bind_param[$i];
    }

    $params[] = $param_type;
    for($i = 0; $i < $n; $i++) {
      $params[] = $array_param[$i];
    }

    $stmt = $mysqli->prepare($query);

    call_user_func_array(array($stmt, 'bind_param'), $params);

    $stmt->execute();

Error :

PHP Warning:  Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given
0

1 Answer 1

1

Try putting references into the array instead of values (notice the ampersands):

$params[] = &$param_type;
for($i = 0; $i < $n; $i++) {
  $params[] = &$array_param[$i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

wow. i just missed out the & symbol. i never know what is the use of & to be put at the front of array variable.
@ggDeGreat You can read more about references in the PHP Manual. If this has answered your question, please mark it as accepted.

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.