Connect with PHP

You can connect to SingleStore via PHP using PHP Data Objects (PDO) or MySQLi.

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,


To read the connection information from the environment, use the $_SERVER['VAR_NAME'] notation.

$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,


Last modified: April 7, 2023

Was this article helpful?