0

Given $foobar = 'a', The following bindParam lines fail:

$stmt->bindParam(':foobar', 'C' . $foobar, PDO::PARAM_STR);

and

$stmt->bindParam(':foobar', "C$foobar", PDO::PARAM_STR);

Both of these return:

PHP Fatal error:  Cannot pass parameter 1 by reference

In contrast, this works fine:

$_foobar = 'C' . $foobar;
$stmt->bindParam(':foobar', $_foobar, PDO::PARAM_STR);

Why is PDO not allowing me to concatenate a string in bindParam?

a coder
  • 7,530
  • 20
  • 84
  • 131
  • 1
    *Probably* because `char` is a reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html - Most likely why and how you're using it elsewhere in your query which isn't shown, so that's a possibility. – Funk Forty Niner Nov 18 '14 at 18:21
  • 3
    Do you understand what "pass-by-reference" is? Use `bindValue` instead of `bindParam` unless you need that specific behaviour. – DCoder Nov 18 '14 at 18:22
  • @Fred, "char" is just my example text. The variable name is something else. I will update the question to avoid confusion. – a coder Nov 18 '14 at 18:23
  • Then it's as DCoder said. People do use reserved words in their query. – Funk Forty Niner Nov 18 '14 at 18:24
  • 1
    The question is not a duplicate per se, but the answers to it should illuminate what's wrong here. – deceze Nov 18 '14 at 18:25
  • DCoder has me pointed on the right track. I'm reading this question now: http://stackoverflow.com/questions/1179874/pdo-bindparam-versus-bindvalue – a coder Nov 18 '14 at 18:26
  • Oh, so now it's `:foobar` lol / That's a big Ten-four. – Funk Forty Niner Nov 18 '14 at 18:26
  • I'm learning PDO - newb mistake. Thanks. – a coder Nov 18 '14 at 18:28

0 Answers0