PDO::prepare
Prepares a statement for execution.
The Microsoft Drivers for PHP for SQL Server does not evaluate prepared statements until execution.
The following table lists the possible key_pair values.
|
Key |
Description |
|---|---|
|
PDO::ATTR_CURSOR |
For more information, see PDOStatement::setAttribute. |
|
PDO::SQLSRV_ATTR_ENCODING |
PDO::SQLSRV_ENCODING_UTF8 (default) PDO::SQLSRV_ENCODING_SYSTEM PDO::SQLSRV_ENCODING_BINARY |
|
PDO::SQLSRV_ATTR_DIRECT_QUERY |
When True, specifies direct query execution. False means prepared statement execution. For more information about PDO::SQLSRV_ATTR_DIRECT_QUERY, see Direct Statement Execution and Prepared Statement Execution in the PDO_SQLSRV Driver. |
|
PDO::SQLSRV_ATTR_QUERY_TIMEOUT |
For more information, see PDO::setAttribute. |
You can close a PDOStatement object by setting it to null.
Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.
This example shows how to use the PDO::prepare method with parameter markers and a forward-only cursor.
<?php $database = "Test"; $server = "(local)"; $conn = new PDO( "sqlsrv:server=$server ; Database = $database", "", ""); $col1 = 'a'; $col2 = 'b'; $query = "insert into Table1(col1, col2) values(?, ?)"; $stmt = $conn->prepare( $query, array( PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY, PDO::SQLSRV_ATTR_QUERY_TIMEOUT => 1 ) ); $stmt->execute( array( $col1, $col2 ) ); print $stmt->rowCount(); echo "\n"; $query = "insert into Table1(col1, col2) values(:col1, :col2)"; $stmt = $conn->prepare( $query, array( PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY, PDO::SQLSRV_ATTR_QUERY_TIMEOUT => 1 ) ); $stmt->execute( array( ':col1' => $col1, ':col2' => $col2 ) ); print $stmt->rowCount(); $stmt = null ?>
This example shows how to use the PDO::prepare method with a scrollable cursor.
<?php
$database = "AdventureWorks";
$server = "(local)";
$conn = new PDO( "sqlsrv:server=$server ; Database = $database", "", "");
$query = "select * from Person.ContactType";
$stmt = $conn->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
echo "\n";
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print "$row[Name]\n";
}
echo "\n..\n";
$row = $stmt->fetch( PDO::FETCH_BOTH, PDO::FETCH_ORI_FIRST );
print_r($row);
$row = $stmt->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 1 );
print "$row[Name]\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT );
print "$row[1]\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR );
print "$row[1]..\n";
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, 0 );
print_r($row);
$row = $stmt->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_LAST );
print_r($row);
?>