Skip to main content

Connect to the Aiven for ClickHouse® service with PHP

Learn how to connect to your Aiven for ClickHouse® service with PHP using the PHP ClickHouse client and the HTTPS port.

Prerequisites

tip

You can install smi2/phpclickhouse with the following command:

composer require smi2/phpclickhouse

or

php composer.phar require smi2/phpclickhouse

Identify connection information

To run the code for connecting to your service, first identify values of the following variables:

VariableDescription
httpsRequired to be set to true
hostHost for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC
portPort for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC
usernameUser for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC
passwordPassword for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC
databaseDatabase Name in the ClickHouse service available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC

Connect to the service

Replace the placeholders in the code with meaningful information on your service connection and run the code.

<?php
require_once 'vendor/autoload.php';
$db = new ClickHouseDB\Client([
'https' => true,
'host' => 'HOSTNAME',
'port' => 'HTTPS_PORT',
'username' => 'USERNAME',
'password' => 'PASSWORD'
]);
$db->database('DATABASE');
$response = $db->select('SELECT 1');
print_r($response->rows());

Now you have your service connection set up and you can proceed to uploading data into your database.