# PDO

> This is an extension of the default PDO class of MSSQL and DBLIB drivers.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › [mssql](/docs/api/yii/db/mssql/index.md) › PDO

| class | `yii\db\mssql\PDO` |
| --- | --- |
| Extends | [`PDO`](https://www.php.net/manual/en/class.pdo.php) |
| Since | 2.0 |
| Source | [`framework/db/mssql/PDO.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/mssql/PDO.php#L26) |

This is an extension of the default PDO class of MSSQL and DBLIB drivers.

It provides workarounds for improperly implemented functionalities of the MSSQL and DBLIB drivers.

The [`SQLSRV_ATTR_ENCODING`](/docs/api/yii/db/mssql/pdo/index.md#const-SQLSRV_ATTR_ENCODING) and [`SQLSRV_ENCODING_SYSTEM`](/docs/api/yii/db/mssql/pdo/index.md#const-SQLSRV_ENCODING_SYSTEM) constants can be used in [`Connection::$attributes`](/docs/api/yii/db/connection/index.md#property-attributes)
to eliminate the implicit `nvarchar` conversion overhead on `char`/`varchar` indexed columns.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`beginTransaction()`](#method-beginTransaction) | Starts a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support transactions. | `PDO` |
| [`commit()`](#method-commit) | Commits a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support transactions. | `PDO` |
| [`getAttribute()`](#method-getAttribute) | Retrieve a database connection attribute. | `PDO` |
| [`lastInsertId()`](#method-lastInsertId) | Returns value of the last inserted ID. | `PDO` |
| [`rollBack()`](#method-rollBack) | Rollbacks a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support transactions. | `PDO` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-SQLSRV_ATTR_ENCODING"></span>`SQLSRV_ATTR_ENCODING` | `1000` | A constant for the pdo_sqlsrv encoding attribute. Pass as a key in [`Connection::$attributes`](/docs/api/yii/db/connection/index.md#property-attributes). | `PDO` |
| <span id="const-SQLSRV_ENCODING_SYSTEM"></span>`SQLSRV_ENCODING_SYSTEM` | `3` | A constant for ANSI (system code page) string encoding. Use with [`SQLSRV_ATTR_ENCODING`](/docs/api/yii/db/mssql/pdo/index.md#const-SQLSRV_ATTR_ENCODING) to avoid the implicit `nvarchar`-to-`varchar` conversion that degrades performance on `char`/`varchar` indexed columns. | `PDO` |
| <span id="const-SQLSRV_ENCODING_UTF8"></span>`SQLSRV_ENCODING_UTF8` | `65001` | A constant for UTF-8 string encoding. This is the pdo_sqlsrv default for [`PDO::PARAM_STR`](https://www.php.net/manual/en/class.pdo.php) parameters. | `PDO` |

## Method details

### beginTransaction() <span id="method-beginTransaction"></span>

```php
public function beginTransaction(): bool
```

Starts a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support
transactions.

**Returns**

`bool`: The result of a transaction start.

### commit() <span id="method-commit"></span>

```php
public function commit(): bool
```

Commits a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support
transactions.

**Returns**

`bool`: The result of a transaction commit.

### getAttribute() <span id="method-getAttribute"></span>

```php
public function getAttribute(int $attribute): mixed
```

Retrieve a database connection attribute.

It is necessary to override PDO's method as some MSSQL PDO driver (for example, dblib) does not support getting
attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attribute` | `int` | One of the PDO::ATTR_* constants. |

**Returns**

`mixed`: A successful call returns the value of the requested PDO attribute. An unsuccessful call returns `null`.

### lastInsertId() <span id="method-lastInsertId"></span>

```php
public function lastInsertId(string|null $sequence = null): string|false
```

Returns value of the last inserted ID.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$sequence` | `string\|null` | The sequence name. Defaults to `null`. |

**Returns**

`string|false`: Last inserted ID value.

### rollBack() <span id="method-rollBack"></span>

```php
public function rollBack(): bool
```

Rollbacks a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support
transactions.

**Returns**

`bool`: The result of a transaction roll back.