1

I had an issue where I executed a stored procedure with variables:

INSERT INTO table_walkinleads
VALUES('', FirstName, LastName, Phone, Email, NOW())

but although this was working perfectly in phpmyadmin. It was not the case in my php code.

And the MAIN reason was because of this.

foreach($params as $name => $value ) {   
    $this->query->bindParam($name,$value);
}

Look carefully at the $value variable in the foreach statement. Before was like that. But after some investigation, I come to realize it needed to be &$value. But WHY? What is the difference? Why just adding that reference in the value variable makes the difference in making my stored procedure work correctly?

foreach($params as $name => &$value ) {   
    $this->query->bindParam($name,$value);
}

PS: the error was still executing the stored procedure, BUT the last variable was inserted as the same value for ALL the variables in the stored procedure. Making it look like I just entered the SAME variable for all the stored procedure variables.

Does anyone have an explanation for this behavior and the reason behind it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
S.H.
  • 2,833
  • 3
  • 26
  • 28
  • You ought to use `->bindValue` here. `bindParam` is for output values, thus needs a referenceable variable (which the foreach always referring to the last loops value breaks due to a deferred execute). See also http://stackoverflow.com/questions/14413326/confusion-between-bindvalue-and-bindparam – mario Oct 09 '13 at 03:48
  • makes sense the bindvalye method... but by just adding the reference it works like a charm with bindparam. I mean Its not big deal because it worked right? its all I needed, but my mind still wonders – S.H. Oct 09 '13 at 03:58

0 Answers0