0

i am trying this code, but i get this error:

Only variables can be passed by reference in xxx

script

class page {
  function insert($db, $of, $form, &$arr) {

      $i = 0;

      foreach(array_combine($form['value0'], $arr) as $val=>$v){

          $sql->prepare("mysqli query here");
          $sql->bind_param('ssss', $val, $of, $v[$i][0], $v[$i][1]);//error here
          $sql->execute();
          $i++;

      }
      return true;
  }
}

what is the reason, and how can be solved ? thanks

4
  • 2
    I don't even see where sql is initialized Commented Oct 27, 2011 at 2:28
  • On your error line, should $of be $oferta? Commented Oct 27, 2011 at 2:29
  • Why do you think that would fix it? Commented Oct 27, 2011 at 2:30
  • $of, but for this case, is irrelevant, the problem is about reference (&$arr). Commented Oct 27, 2011 at 2:31

1 Answer 1

5

I assume you're using mysqli::bind_param. All arguments except the first are passed by reference. This means they must be variables, and not strings, array elements, etc. I'm actually not sure why it needs to do this by reference, but never mind. You can fix it pretty easily:

$v0 = $v[$i][0];
$v1 = $v[$i][1];
$sql->bind_param('ssss', $val, $of, $v0, $v1);
Sign up to request clarification or add additional context in comments.

1 Comment

"Silly design decision", but it is what it is.: I suppose "so the value can be assigned to after bind but before execution?" which would allow the same $sql to be re-used repeatedly by only changing the value of the referenced variables... again, not how I would have designed it.

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.