PHP code example of itstably / clickhouse-php-client
1. Go to this page and download the library: Download itstably/clickhouse-php-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
itstably / clickhouse-php-client example snippets
$server = new ItStably\Clickhouse\Server('127.0.0.1', '8123', 'default', 'user', 'pass');
$serverProvider = (new ItStably\Clickhouse\ServerProvider())->addServer($server);
$client = new ItStably\Clickhouse\Client($serverProvider);
$result = $client->readOne('select number from system.numbers limit 100');
foreach ($result as $number) {
echo $number['number'].PHP_EOL;
}
['UInt64']
$result = $client->readOne('select number from system.numbers where number in _numbers limit 100', new TempTable('_numbers', 'numbers.csv', [
'number' => 'UInt64'
]));
foreach ($result as $number) {
echo $number['number'].PHP_EOL;
}
list($clicks, $visits, $views) = $client->read([
['query' => "select * from clicks where date = '2017-01-01'"],
['query' => "select * from visits where date = '2017-01-01'"],
['query' => "select * from views where date = '2017-01-01'"],
]);
foreach ($clicks as $click) {
echo $click['date'].PHP_EOL;
}
list($clicks, $visits, $views) = $client->read([
['query' => "select * from clicks where date = '2017-01-01' and userId in _users", new TempTable('_users', 'users.csv', ['number' => 'UInt64'])],
['query' => "select * from visits where date = '2017-01-01'"],
['query' => "select * from views where date = '2017-01-01'"],
]);
foreach ($clicks as $click) {
echo $click['date'].PHP_EOL;
}
$client->writeOne("insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)");
$client->write([
['query' => "insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)"],
['query' => "insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)"],
['query' => "insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)"]
]);
$client->writeFiles('table', ['date', 'column'], [
new ItStably\Clickhouse\Common\File('/file-1.csv'),
new ItStably\Clickhouse\Common\File('/file-2.csv')
]);
$client->insertFiles('table', ['date', 'column'], [
new ItStably\Clickhouse\Common\File('/file-1.tsv'),
new ItStably\Clickhouse\Common\File('/file-2.tsv')
], ItStably\Clickhouse\Common\Format::TSV);
$client->writeOne('DROP TABLE table');
bash
composer
file-1.tsv
file-2.tsv
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.