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,

Note

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

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

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

Last modified: April 7, 2023

Was this article helpful?