# Connect with PHP

You can connect to SingleStore via PHP using [PHP Data Objects (PDO)](https://www.php.net/manual/en/book.pdo.php) or [MySQLi](https://www.php.net/manual/en/book.mysqli.php).&#x20;

To connect using PDO, you need to create a DSN from the host, port, and database name, and then pass the DSN to the PDO constructor along with the database username and password. For example,

> **📝 Note**: To read the connection information from the environment, use the `$_SERVER['VAR_NAME']` notation.

```php
<?php
    $host = $_SERVER['HOSTNAME'];
    $port = $_SERVER['PORT'];
    $user = $_SERVER['USERNAME'];
    $password = $_SERVER['PASSWORD'];
    $db = $_SERVER['DB_NAME'];
    $charset = 'utf8';

    $dsn = "mysql:host={$host};port={$port};dbname={$db};charset={$charset}";
    $pdo = new PDO($dsn, $user, $password);
?>

```

To connect using MySQLi, pass the host, username, password, database name, and port to the `mysqli` constructor. For example,

```php
$link = new mysqli($_SERVER['HOSTNAME'], $_SERVER['USERNAME'], $_SERVER['PASSWORD'], $_SERVER['DB_NAME'], $_SERVER['PORT']);
```

***

Modified at: March 3, 2025

Source: [/db/v9.1/developer-resources/connect-with-application-development-tools/connect-with-php/](https://docs.singlestore.com/db/v9.1/developer-resources/connect-with-application-development-tools/connect-with-php/)

(An index of the documentation is available at /llms.txt)
