tags:

views:

1107

answers:

2

What is the difference between bindParam and bindValue for PDO in PHP?

http://www.php.net/manual/en/pdostatement.bindparam.php

http://www.php.net/manual/en/pdostatement.bindvalue.php

+7  A: 

Here are some I can think about :

  • With bindParam, you can only pass variables ; not values
  • with bindValue, you can pass both (values, obviously, and variables)
  • bindParam works only with variables because it allows parameters to be given as input/output, by "reference" (and a value is not a valid "reference" in PHP) : it is useful with drivers that (quoting the manual) :

support the invocation of stored procedures that return data as output parameters, and some also as input/output parameters that both send in data and are updated to receive it.

With some DB engines, stored procedures can have parameters that can be used for both input (giving a value from PHP to the procedure) and ouput (returning a value from the stored proc to PHP) ; to bind those parameters, you've got to use bindParam, and not bindValue.

Pascal MARTIN
thanks, can only accept one though
koen
you're welcome :-) (don't worry about that ^^ )
Pascal MARTIN
+11  A: 

The answer is in the documentation for bindParam:

Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

acrosman
thanks, have missed that
koen