PHP Reference ยท Databases (PDO)
PHP bindParam() and bindValue() methods
Reference for PDOStatement::bindValue() and bindParam(): bind values to placeholders, set PDO::PARAM_* types, and learn why bindParam() binds a reference.
PDOStatement::bindValue() and PDOStatement::bindParam() attach a value to a placeholder of a prepared statement before you call execute(). They are the explicit alternative to passing an array to execute(), and they let you state the data type of each value.
The difference is timing. bindValue() copies the value now. bindParam() binds the variable itself by reference, and its value is read at the moment of execute(). Most code should use bindValue().
$stmt = $pdo->prepare('SELECT * FROM post WHERE author_id = :author LIMIT :n'); $stmt->bindValue(':author', $authorId, PDO::PARAM_INT); $stmt->bindValue(':n', 10, PDO::PARAM_INT); $stmt->execute();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
PDOStatement::bindValue(string|int $param, mixed $value, int $type = PDO::PARAM_STR): bool PDOStatement::bindParam( string|int $param, mixed &$var, int $type = PDO::PARAM_STR, int $maxLength = 0, mixed $driverOptions = null ): bool
| Name | Description |
|---|---|
$param |
The placeholder: ':name' for named placeholders, or a 1-based position for ?. |
$value / &$var |
The value (bindValue) or the variable bound by reference (bindParam). |
$type |
A PDO::PARAM_* constant: PARAM_STR (default), PARAM_INT, PARAM_BOOL, PARAM_NULL, PARAM_LOB. |
$maxLength |
For output parameters of stored procedures; normally left at 0. |
$driverOptions |
Driver-specific options; rarely used. |
Return value: true on success, false on failure (when the error mode is not exception mode).
Errors / Exceptions: PDOException if the placeholder does not exist or the driver rejects the binding. Passing a literal to bindParam() throws an Error because the argument must be a variable.
Usage notes
- Positions start at 1, not 0, in
bindValue(1, ...). In the array passed toexecute()positions are plain list keys (0, 1, ...). - Pick
bindValue()by default. UsebindParam()only for the loop pattern shown above, where one variable changes betweenexecute()calls. bindParam()and loops. If you bind a loop variable by reference and keep using it after the loop, you may be changing the value the nextexecute()will send. Bind again or usebindValue()when in doubt.- Set
PARAM_INTfor numbers that must stay numbers, especially forLIMITandOFFSETon MySQL with emulated prepares, where a quoted string'10'is a syntax error. PARAM_NULLsends SQLNULL. A PHPnullpassed with the default type also becomesNULLin most drivers, but stating it is clearer.PARAM_BOOLturnstrue/falseinto 1/0 (or the driver's boolean type).- Large objects use
PARAM_LOBwith a stream resource. execute($array)is shorter when all values are strings or the database converts them. It cannot set types.- Binding is not escaping. The value is never inserted into the SQL text, which is what protects against SQL injection.