bindParam and bindValue

bindValue(),

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

bindParam()

  • call PDOStatement::bindParam() to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers

PHP PDOStatement: bindParam vs bindValue

  • It is an ordinary place to repeatedly execute a query, with each iteration using different parameters.
  • However, using the conventional query() method and a looping mechanism comes at a cost of both overhead, because of the repeated parsing of more or less identical query for validity, and coding convenience, and the need to repeatedly reconfigure the query using the new values for each iteration.
  • Using prepared statement eliminate this problem. It is easy to accomplish in PHP using the PDOStatement class.
  • The bindValue and bindParam PDOStatement methods are both use in binding a variable to a parameter (name or question mark placeholder use in SQL statement).
  • The binds a parameter exclusively to a specified variable name which is bindParam.
  • Bound as a reference while the binds a value which could be a variable, bindValue.
  • An integer or string to a parameter.
[ad type=”banner”]

To make the difference clearer, we can see the code examples:

  • Both bind a variable to a placeholder’s parameter.
  • In Both bindParam and bindValue, a variable can be binded to a parameter.
[pastacode lang=”php” manual=”%3C%20%3Fphp%0A%24name%20%3D%20’collins’%3B%0A%24sql%20%3D%20’INSERT%20INTO%20feeds%20(name)%20VALUES%20(%3Aname)’%3B%0A%24update%20%3D%20%24conn%20-%3E%20prepare(%24sql)%3B%0A%24update%20-%3E%20bindParam(‘%3Aname’%2C%20%24name)%3B%0A%24update%20-%3E%20execute()%3B%0A” message=”php code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”%3C%20%3Fphp%0A%24name%20%3D%20’collins’%3B%0A%24sql%20%3D%20’INSERT%20INTO%20feeds%20(name)%20VALUES%20(%3Aname)’%3B%0A%24update%20%3D%20%24conn%20-%3E%20prepare(%24sql)%3B%0A%24update%20-%3E%20bindValue(‘%3Aname’%2C%20%24name)%3B%0A%24update%20-%3E%20execute()%3B%0A” message=”php code” highlight=”” provider=”manual”/]
  • bindParam binds variable as a reference.
  • For example, when a variable is assigned as a reference to another variable, a change in the value of the variable assigned as a reference also affect the parent variable.
  • For creating reference, the & is used

Example

[pastacode lang=”javascript” manual=”%0A%24a%20%3D%20’good%20boy’%3B%0A%24b%20%3D%20%26%24a%3B%20%20%2F%2F%20%24a%20and%20%24b%20both%20equal%20%22good%20boy%22%0A%24b%20%3D%20%22bad%20boy%22%3B%20%2F%2F%20%24a%20and%20%24b%20both%20equal%20%22bad%20boy%22%0Aecho%20%22%24a%22%3B%20%2F%2F%20echos%20%22bad%20boy%22%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • While checking bindParam description, you could see that the method’s second argument accepts a variable that is pass as a reference and will only be evaluated at the time that the execute() is called.

Thus, we can still change the value of a variable bonded to a parameter even after the bindParam() method had been called like so:

[pastacode lang=”javascript” manual=”%24name%20%3D%20’collins’%3B%0A%24sql%20%3D%20’INSERT%20INTO%20feeds%20(name)%20VALUES%20(%3Aname)’%3B%0A%24update%20%3D%20%24conn%20-%3E%20prepare(%24sql)%3B%0A%24update%20-%3E%20bindParam(‘%3Aname’%2C%20%24name)%3B%0A%24name%20%3D%20’john’%3B%0A%24update%20-%3E%20execute()%3B%20%2F%2F%20execute%20with%20john%20inserted%20into%20the%20column%20%22name%22%0A” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • Above code will bind ‘john’ to the :name parameter because, as we mentioned, variable are binded by reference when using bindParam.
  • “bindValue” accept different data types.
  • Unlike the bindParam() that binds only a variable name to a parameter, with bindValue, you can bind a variable and also an integer, float, and string.

Binding an integer to a parameter via “bindValue”.

[pastacode lang=”php” manual=”%3C%20%3Fphp%0A%24sql%20%3D%20’INSERT%20INTO%20feeds%20(numbers)%20VALUES%20(%3Anumber)’%3B%0A%24update%20%3D%20%24conn%20-%3E%20prepare(%24sql)%3B%0A%24update%20-%3E%20bindValue(‘%3Anumber’%2C%20100)%3B%0A%24update%20-%3E%20execute()%3B%0A” message=”php code” highlight=”” provider=”manual”/]

Binding a string to a parameter via “bindValue”.

[pastacode lang=”php” manual=”%3C%20%3Fphp%0A%24sql%20%3D%20’INSERT%20INTO%20feeds%20(names)%20VALUES%20(%3Aname)’%3B%0A%24update%20%3D%20%24conn%20-%3E%20prepare(%24sql)%3B%0A%24update%20-%3E%20bindValue(‘%3Aname’%2C%20’collins’)%3B%0A%24update%20-%3E%20execute()%3B%0A” message=”php code” highlight=”” provider=”manual”/]
  • While you are trying to bind say, a string or number (float / integer) to a parameter using bindParam, you’ll get this error:

“Fatal error: Cannot pass parameter 2 by reference”

 

[ad type=”banner”]

Categorized in: