2022-06-07 03:32:51 +00:00
---
2023-11-29 14:29:30 +00:00
title: PHP Client Library
2023-02-05 16:07:16 +00:00
sidebar_label: PHP
2023-11-29 14:29:30 +00:00
description: This document describes the TDengine PHP client library.
2022-06-07 03:32:51 +00:00
---
2023-11-29 14:29:30 +00:00
`php-tdengine` is the TDengine PHP client library provided by TDengine community. In particular, it supports Swoole coroutine.
2022-06-07 03:32:51 +00:00
2023-11-29 14:29:30 +00:00
PHP client library relies on TDengine client driver.
2022-06-07 03:32:51 +00:00
2024-01-19 08:49:46 +00:00
Project Repository: [https://github.com/Yurunsoft/php-tdengine](https://github.com/Yurunsoft/php-tdengine)
2022-06-07 03:32:51 +00:00
After TDengine client or server is installed, `taos.h` is located at:
2023-05-15 04:26:03 +00:00
- Linux: `/usr/local/taos/include`
- Windows: `C:\TDengine\include`
- macOS: `/usr/local/include`
2022-06-07 03:32:51 +00:00
TDengine client driver is located at:
- Linux: `/usr/local/taos/driver/libtaos.so`
- Windows: `C:\TDengine\taos.dll`
2023-05-15 04:26:03 +00:00
- macOS: `/usr/local/lib/libtaos.dylib`
2022-06-07 03:32:51 +00:00
## Supported Platforms
2023-05-15 04:26:03 +00:00
- Windows, Linux, and macOS
2022-06-07 03:32:51 +00:00
- PHP >= 7.4
- TDengine >= 2.0
- Swoole >= 4.8 (Optional)
## Supported Versions
Because the version of TDengine client driver is tightly associated with that of TDengine server, it's strongly suggested to use the client driver of same version as TDengine server, even though the client driver can work with TDengine server if the first 3 sections of the versions are same.
## Installation
### Install TDengine Client Driver
2024-05-13 06:24:25 +00:00
Regarding how to install TDengine client driver please refer to [Install Client Driver](../#install-client-driver)
2022-06-07 03:32:51 +00:00
### Install php-tdengine
2023-05-15 04:26:03 +00:00
**Download Source Code Package and Unzip: **
2022-06-07 03:32:51 +00:00
```shell
curl -L -o php-tdengine.tar.gz https://github.com/Yurunsoft/php-tdengine/archive/refs/tags/v1.0.2.tar.gz \
&& mkdir php-tdengine \
&& tar -xzf php-tdengine.tar.gz -C php-tdengine --strip-components=1
```
2023-05-15 04:26:03 +00:00
**Non-Swoole Environment: **
2022-06-07 03:32:51 +00:00
```shell
phpize && ./configure && make -j && make install
```
2023-05-15 04:26:03 +00:00
**Specify TDengine location: **
2022-06-07 03:32:51 +00:00
```shell
2022-08-22 07:31:39 +00:00
phpize && ./configure --with-tdengine-dir=/usr/local/Cellar/tdengine/3.0.0.0 && make -j && make install
2022-06-07 03:32:51 +00:00
```
> `--with-tdengine-dir=` is followed by TDengine location.
> It's useful in case TDengine installatio location can't be found automatically or MacOS.
2023-05-15 04:26:03 +00:00
**Swoole Environment: **
2022-06-07 03:32:51 +00:00
```shell
phpize && ./configure --enable-swoole && make -j && make install
```
**Enable Extension:**
Option One: Add `extension=tdengine` in `php.ini`.
Option Two: Use CLI `php -dextension=tdengine test.php`.
## Sample Programs
2023-11-29 14:29:30 +00:00
In this section a few sample programs which use TDengine PHP client library to access TDengine cluster are demonstrated.
2022-06-07 03:32:51 +00:00
> Any error would throw exception: `TDengine\Exception\TDengineException`
2023-03-03 02:14:57 +00:00
### Establish Connection
2022-06-07 03:32:51 +00:00
<details>
<summary>Establish Connection</summary>
```c
2022-06-13 07:32:44 +00:00
{{#include docs/examples/php/connect.php}}
2022-06-07 03:32:51 +00:00
```
</details>
### Insert Data
<details>
<summary>Insert Data</summary>
```c
2022-06-13 07:32:44 +00:00
{{#include docs/examples/php/insert.php}}
2022-06-07 03:32:51 +00:00
```
</details>
### Synchronous Query
<details>
<summary>Synchronous Query</summary>
```c
2022-06-13 07:32:44 +00:00
{{#include docs/examples/php/query.php}}
2022-06-07 03:32:51 +00:00
```
</details>
### Parameter Binding
<details>
<summary>Parameter Binding</summary>
```c
2022-06-13 07:32:44 +00:00
{{#include docs/examples/php/insert_stmt.php}}
2022-06-07 03:32:51 +00:00
```
</details>
## Constants
2022-06-07 03:41:14 +00:00
| Constant | Description |
2022-06-07 03:32:51 +00:00
| ----------------------------------- | ----------- |
| `TDengine\TSDB_DATA_TYPE_NULL` | null |
| `TDengine\TSDB_DATA_TYPE_BOOL` | bool |
| `TDengine\TSDB_DATA_TYPE_TINYINT` | tinyint |
| `TDengine\TSDB_DATA_TYPE_SMALLINT` | smallint |
| `TDengine\TSDB_DATA_TYPE_INT` | int |
| `TDengine\TSDB_DATA_TYPE_BIGINT` | bigint |
| `TDengine\TSDB_DATA_TYPE_FLOAT` | float |
| `TDengine\TSDB_DATA_TYPE_DOUBLE` | double |
| `TDengine\TSDB_DATA_TYPE_BINARY` | binary |
| `TDengine\TSDB_DATA_TYPE_TIMESTAMP` | timestamp |
| `TDengine\TSDB_DATA_TYPE_NCHAR` | nchar |
| `TDengine\TSDB_DATA_TYPE_UTINYINT` | utinyint |
| `TDengine\TSDB_DATA_TYPE_USMALLINT` | usmallint |
| `TDengine\TSDB_DATA_TYPE_UINT` | uint |
| `TDengine\TSDB_DATA_TYPE_UBIGINT` | ubigint |