Description

Binds a value to a corresponding named or question mark placeholder
in the SQL statement that was used to prepare the statement.

Parameters

parameter

Parameter identifier. For a prepared statement using named
placeholders, this will be a parameter name of the form
:name. For a prepared statement using
question mark placeholders, this will be the 1-indexed position of
the parameter.

When binding parameters, apparently you can't use a placeholder twice (e.g. "select * from mails where sender=:me or recipient=:me"), you'll have to give them different names otherwise your query will return empty handed (but not fail, unfortunately). Just in case you're struggling with something like this.

Although bindValue() escapes quotes it does not escape "%" and "_", so be careful when using LIKE. A malicious parameter full of %%% can dump your entire database if you don't escape the parameter yourself. PDO does not provide any other escape method to handle it.

Note that the third parameter ($data_type) in the majority of cases will not type cast the value into anything else to be used in the query, nor will it throw any sort of error if the type does not match up with the value provided. This parameter essentially has no effect whatsoever except throwing an error if it is set and is not a float, so do not think that it is adding any extra level of security to the queries.

The two exceptions where type casting is performed:

- if you use PDO::PDO_PARAM_INT and provide a boolean, it will be converted to a long- if you use PDO::PDO_PARAM_BOOL and provide a long, it will be converted to a boolean

The reason that we cannot define the value variable for bindValue() after calling it, is because that it binds the value to the prepared statement immediately and does not wait until the execute() to happen.

The following code will issue a notice and prevent the query from taking place:<?php $st = $db->prepare ("SELECT * FROM posts WHERE id= :val ");$st->bindValue(':val',$val);

Whereas in the case of bindParam, the evaluation of the value to the parameter will not be performed until the call of execute(). And that's to gain the benefit of reference passing.<?php $st = $db->prepare ("SELECT * FROM posts WHERE id = :val ");$st->bindParam(':val',$val);