{question}
How to connect to Singlestore programatically using PHP Data Objects (PDO) interface ?
{question}
{answer}
SingleStore is MySQL wire protocol compliant, and most connection mechanisms work without modification, such as for the PHP PDO driver. The following is a code sample to setup the connection:
Sample code
<html>
<head>
<title>Singlestore PHP Test</title>
</head>
<body>
<?php
// Replace the below values
$host = '127.0.0.1';
$username = 'sdbuser1';
$password = 'SDBuserPass!';
$db = 'SinglestoreTestDB';
try {
echo "Connecting to $host... ";
$conn = new PDO("mysql:host=$host;dbname=$db", $username, $password);
// PDO error mode set to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
</body>
</html>
If the only message you see is "Connecting to $host...", this message might indicate that the php-mysql package is not installed. Refer to your distribution instructions on how to install the php-mysql package.
{answer}