PHP连接ClickHouse数据库

简介

ClickHouse提供了很多第三方开发库:
https://clickhouse.com/docs/zh/interfaces/third-party/client-libraries

我这里使用的是:
https://packagist.org/packages/smi2/phpClickHouse

安装

composer require smi2/phpclickhouse

使用示例

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);

require_once('vendor/autoload.php');
use ClickHouseDB;

$config = [
    'host' => '127.0.0.1',
    'port' => '18123',
    'username' => 'default',
    'password' => ''
];
$db = new ClickHouseDB\Client($config);
$db->database('tutorial');
$db->setTimeout(1.5);      // 1 second , support only Int value
$db->setTimeout(10);       // 10 seconds
$db->setConnectTimeOut(5); // 5 seconds
$db->ping(true); // if can`t connect throw exception

//print_r($db->showTables());

$statement = $db->select('SELECT * FROM dish LIMIT 2');

// Count select rows
echo 'count:'.$statement->count();
echo '<hr>';

foreach ($statement as $key=>$value) {
    echo $value['id'].':'.$value['name'];
    echo '<br>';
}

发表评论

邮箱地址不会被公开。 必填项已用*标注