PHPackages                             ierusalim/php-clickhouse - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. ierusalim/php-clickhouse

ActiveLibrary[Database &amp; ORM](/categories/database)

ierusalim/php-clickhouse
========================

ClickHouse simple access library (http, https)

0.2.4(6y ago)63.4k2Apache-2.0PHPPHP &gt;=5.5

Since Jul 17Pushed 6y ago2 watchersCompare

[ Source](https://github.com/ierusalim/php-clickhouse)[ Packagist](https://packagist.org/packages/ierusalim/php-clickhouse)[ RSS](/packages/ierusalim-php-clickhouse/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (1)Versions (14)Used By (0)

php-clickhouse
==============

[](#php-clickhouse)

[![Build Status](https://camo.githubusercontent.com/c959d201d25bd774a1828e14205511cec6c2801221d5d063946bc33ae330d594/68747470733a2f2f6170692e7472617669732d63692e6f72672f6965727573616c696d2f7068702d636c69636b686f7573652e7376673f6272616e63683d6d6173746572)](https://www.travis-ci.org/ierusalim/php-clickhouse)[![codecov](https://camo.githubusercontent.com/313a4846ba75cc0ee285cdf3f250a7a45885912cbaa999182783edce2c6a0568/68747470733a2f2f636f6465636f762e696f2f67682f6965727573616c696d2f7068702d636c69636b686f7573652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/ierusalim/php-clickhouse)

ClickHouse simple access library (http, https)
----------------------------------------------

[](#clickhouse-simple-access-library-http-https)

### Class **ClickHouseAPI**

[](#class-clickhouseapi)

Class **ClickHouseAPI** contains simple http/https connector for ClickHouse server and have not dependencies (may be used independently, file src/ClichHouseAPI.php).

#### API simple requests functions:

[](#api-simple-requests-functions)

- **query**($sql \[,$post\_data\]) - object-oriented style SQL-query (return $this, throw exceptions)
- **getQuery**($h\_query \[, $sess\]) - send GET request
- **postQuery**($h\_query, $post\_data \[, $sess\]) - send POST request

#### Async (parallel) requests:

[](#async-parallel-requests)

- Set **toSlot**(name) before any request, and the request will be launched asynchronously.
- **toSlot**("name")-&gt;**query**($sql) - start async-$sql-query, results will be written to slot "name"
- Get results from this slot may at any time later:
- **slotResults**("name") - get results from slot "name".

#### Server-state functions:

[](#server-state-functions)

- **setServerUrl**($url) - set ClickHouse server parameters by url (host, port, etc.)
- **getVersion**() - return version of ClickHouse server (side effect - detect server features)
- **isSupported**(feature-name) - true or false depending on the server support features.

##### Sessions:

[](#sessions)

- **getSession**() - get current session\_id from options
- **setSession**(\[$sess\]) - set session\_id or generate new session\_id and set it

##### Options:

[](#options)

- **setOption**($key, $value) - set http-option for all next requests
- **getOption**($key) - get current http-option value
- **delOption**($key) - delete http-option (same -&gt;setOption($key, null)

### Class **ClickHouseQuery**

[](#class-clickhousequery)

Class **ClickHouseQuery** contains wrapper for ClickHouseAPI and allow to easily send queries to ClickHouse server and parsing answering data.

#### Main query-functions for use:

[](#main-query-functions-for-use)

- **queryFalse**($sql, \[post\])- for queries that should not return anything. False if ok, or error string.
- **queryTrue**($sql, \[post\]) - return false only if error, otherwise return true or response data.
- **queryValue**($sql, \[post\]) - send any query and receive all data in one string (false if error)
- **queryArray**($sql) - for queries returning structured data (usually one or more table rows)
- **queryKeyValues**(see descr.) - for queries returning 2 columns, first means as key, second as value
- **queryInsertArray**($table, $fields\_names, $fields\_set) - insert data into table from array
- **queryInsertFile**($table, $file, $structure) - insert data from file into table
- **queryInsertGzip**($table, $file, $format \[, $fields\]) - insert data from file, using gzip when sending

### Class **ClickHouseFunctions**

[](#class-clickhousefunctions)

Class **ClickHouseFunctions** based on ClickHouseQuery and ClickHouseAPI and contains functions for simple operations with ClickHouse.

#### Functions:

[](#functions)

- **createTableQuick**($table, $fields\_arr) - create table with specified fields
- **sendFileInsert**($file, $table) - send TabSeparated-file into table (structure autodetect)
- **clearTable**($table \[, $sess\]) - clear table (DROP and re-create)
- **dropTable**($table \[, $sess\]) - drop specified table
- **renameTable**($from\_name\_or\_arr \[, $to\_name\] \[, $sess\]) - rename tables
- **getTableFields**($table, ...) - returns \[field\_name=&gt;field\_type\] array
- **getTableInfo**($table \[, $extended\]) - returns array with info about table
- **getTablesList**(\[$db\] \[,$pattern\]) - returns tables list by SHOW TABLES request
- **createDatabase**($db) - create new database with specified name
- **dropDatabase**($db) - drop specified database and remove all tables inside
- **getDatabasesList**() - returns array contained names of existing Databases
- **setCurrentDatabase**($db \[, $sess\]) - set current database by 'USE db' request or by option
- **getCurrentDatabase**(\[$sess\]) - return results of 'SELECT currentDatabase()' or from option
- **getUptime**() - return server uptime in seconds
- **getSystemSettings**() - get information from system.settings as array \[name=&gt;value\]

### Example:

[](#example)

```
