php.mdx 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
---
sidebar_position: 1
sidebar_label: PHP
title: PHP Connector
---

`php-tdengine` is the TDengine PHP connector provided by TDengine community. In particular, it supports Swoole coroutine.

PHP Connector relies on TDengine client driver.

Project Repository:<https://github.com/Yurunsoft/php-tdengine>

After TDengine client or server is installed, `taos.h` is located at:

- Linux:`/usr/local/taos/include`
- Windows:`C:\TDengine\include`

TDengine client driver is located at:

- Linux: `/usr/local/taos/driver/libtaos.so`
- Windows: `C:\TDengine\taos.dll`

## Supported Platforms

- Windows、Linux、MacOS

- 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

Regarding how to install TDengine client driver please refer to [Install Client Driver](/reference/connector#installation-steps)

### Install php-tdengine

**Download Source Code Package and Unzip:**

```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
```

> Version number `v1.0.2` is only for example, it can be replaced to any newer version, please find available versions in [TDengine PHP Connector Releases](https://github.com/Yurunsoft/php-tdengine/releases).

**Non-Swoole Environment:**

```shell
phpize && ./configure && make -j && make install
```

**Specify TDengine location:**

```shell
phpize && ./configure --with-tdengine-dir=/usr/local/Cellar/tdengine/2.4.0.0 && make -j && make install
```

> `--with-tdengine-dir=` is followed by TDengine location.
> It's useful in case TDengine installatio location can't be found automatically or MacOS.

**Swoole Environment:**

```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

In this section a few sample programs which use TDengine PHP connector to access TDengine cluster are demonstrated.

> Any error would throw exception: `TDengine\Exception\TDengineException`

### Establish Conection

<details>
<summary>Establish Connection</summary>

```c
{{#include docs-examples/php/connect.php}}
```

</details>

### Insert Data

<details>
<summary>Insert Data</summary>

```c
{{#include docs-examples/php/insert.php}}
```

</details>

### Synchronous Query

<details>
<summary>Synchronous Query</summary>

```c
{{#include docs-examples/php/query.php}}
```

</details>

### Parameter Binding

<details>
<summary>Parameter Binding</summary>

```c
{{#include docs-examples/php/insert_stmt.php}}
```

</details>

## Constants

### Types

| Type in PHP Connector               | Description |
| ----------------------------------- | ----------- |
| `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     |