| title | PDOStatement::bindValue | Microsoft Docs | |
|---|---|---|
| ms.custom | ||
| ms.date | 01/19/2017 | |
| ms.prod | sql-non-specified | |
| ms.reviewer | ||
| ms.suite | ||
| ms.technology |
|
|
| ms.tgt_pltfrm | ||
| ms.topic | article | |
| ms.assetid | 13bc4ece-420e-4887-8809-bf0705ddf126 | |
| caps.latest.revision | 10 | |
| author | MightyPen | |
| ms.author | genemi | |
| manager | jhubbard |
[!INCLUDEDriver_PHP_Download]
Binds a value to a named or question mark placeholder in the SQL statement.
bool PDOStatement::bindValue( $parameter, $value [,$data_type] );
$parameter: A (mixed) parameter identifier. For a statement using named placeholders, a parameter name (:name). For a prepared statement using the question mark syntax, this will be the 1-based index of the parameter.
$value: The (mixed) value to bind to the parameter.
$data_type: The optional (integer) data type represented by a PDO::PARAM_* constant. The default is PDO::PARAM_STR.
TRUE on success, otherwise FALSE.
Support for PDO was added in version 2.0 of the [!INCLUDEssDriverPHP].
This example shows that after the value of $contact is bound, changing the value does not change the value passed in the query.
<?php
$database = "AdventureWorks";
$server = "(local)";
$conn = new PDO( "sqlsrv:server=$server ; Database = $database", "", "");
$contact = "Sales Agent";
$stmt = $conn->prepare("select * from Person.ContactType where name = ?");
$stmt->bindValue(1, $contact);
$contact = "Owner";
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "$row[Name]\n\n";
}
$stmt = null;
$contact = "Sales Agent";
$stmt = $conn->prepare("select * from Person.ContactType where name = :contact");
$stmt->bindValue(':contact', $contact);
$contact = "Owner";
$stmt->execute();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "$row[Name]\n\n";
}
?>