Search code examples
phppdobindvaluebindparam

PDO bindparam and bindvalue. Which is the diference?


I've read the manual on PHP PDO and have seen this post: PDO PARAM_STR and length

After reading that post I understand that we do not need to specify the length when inserting into DB but specify for outputting.

My question is, can I use bindvalue() instead if I do not wish to specify any length for output? Maybe can someone give a good advice on when is best to user bindparam and bindvalue?

Thanks in advance guys.


Solution

  • You don't have to set the lenght for outputting, you can do this if your want.

    This is what the manual says about the difference between bindValue() and bindParam()

    Binds a PHP variable to a corresponding named or question mark placeholder in the SQL >statement that was used to prepare the statement. Unlike PDOStatement::bindValue(), the >variable is bound as a reference and will only be evaluated at the time that >PDOStatement::execute() is called.

    Most parameters are input parameters, that is, parameters that are used in a read-only >fashion to build up the query. Some drivers 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.

    I use always bindParam when I want to add a variable. But I guess you have to make your own decission for that.