# Using `PDO_MySQL` to Connect to `TiDB`

+ Data source string
  + A string that determines how to connect to the `TiDB` server
  + Format: **`driver:{attr_name}={value};{attr_name}={value} ...`**
  + Example:
    + **`mysql:host=127.0.0.1;dbname=universe;port=4000`**
  + For `TLS` connections, such as connecting to `TiDB Cloud` cluster, additional attributes required:
    + Verify the server certificate or not:**`PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT`**
    + Path to the `CA` root `pem` file:**`PDO::MYSQL_ATTR_SSL_CA`**
+ A `PDO` object as a database handle which is capable of:
  + **`exec():`** Executing statements that do not return a result set, it returns the number of affected rows
  + **`query():`** Retrieving data, it returns a **`PDOStatement`** object upon success or `false` on failure

------------------------------------------------------------------------------
The `PDO_MySQL` module offers similar functionality to `mysqli`. The benefit of learning the PDO approach is that the syntax is more general.

To connect to the TiDB server, instantiate a new PDO object and pass (at a minimum) a data source name and (optionally) a MySQL username, password, and an array of connection options. 

Store the result in a variable, which then becomes the "database handle":

`$dbh = new PDO( string $dsn [, string $username [, string $password [, array $options ]]])`

The data source name is a string that consists of the driver name `(mysql:)` followed by other attributes you specify as name/value pairs, separated by semicolons. Typical attributes include `dbname`, `port`.

The database handle enables you to call the `exec()` and `query()` methods on the PDO object that represents your connection to the database server to execute SQL statements.
