Share via


PDOStatement::setAttribute

Sets an attribute value, either a predefined PDO attribute or a custom driver attribute.

Syntax

bool PDOStatement::setAttribute ($attribute, $value );

Parameters

$attribute: An integer, one of the PDO::ATTR_* or PDO::SQLSRV_ATTR_* constants. See the Remarks section for the list of available attributes.

$value: The (mixed) value to be set for the specified $attribute.

Return Value

TRUE on success, FALSE otherwise.

Remarks

The following table contains the list of available attributes:

Attribute

Values

Description

PDO::ATTR_CURSOR

PDO::CURSOR_FWDONLY

PDO::CURSOR_SCROLL

Specifies server side cursor behaviors. Read only. Default = PDO::CURSOR_FWDONLY.

PDO::CURSOR_SCROLL is the same as SQLSRV_CURSOR_STATIC. See Specifying a Cursor Type and Selecting Rows for more information.

For example, array( PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY ).

PDO::SQLSRV_ATTR_ENCODING

Integer

PDO::SQLSRV_ENCODING_UTF8 (Default)

PDO::SQLSRV_ENCODING_SYSTEM

PDO::SQLSRV_ENCODING_BINARY

Sets the character set encoding to be used by the driver to communicate with the server.

PDO::SQLSRV_ATTR_QUERY_TIMEOUT

Integer

Sets the query timeout in seconds.

By default, the driver will wait indefinitely for results. Negative numbers are not allowed.

0 means no timeout.

Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.

Example

<?php
$database = "AdventureWorks";
$server = "(local)";
$conn = new PDO( "sqlsrv:server=$server ; Database = $database", "", "", array('MultipleActiveResultSets'=>false )  );

$stmt = $conn->prepare('SELECT * FROM Person.ContactType');

$stmt->setAttribute( PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL );
echo $stmt->getAttribute( constant( "PDO::ATTR_CURSOR" ) );

echo "\n";

$stmt->setAttribute( PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY );
echo $stmt->getAttribute( constant( "PDO::ATTR_CURSOR" ) );

echo "\n";

$stmt->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 2);
echo $stmt->getAttribute( constant( "PDO::SQLSRV_ATTR_QUERY_TIMEOUT" ) );
?>

See Also

Reference

PDOStatement Class

Other Resources

PDO