PHP Connector

    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
    • macOS:/usr/local/include

    TDengine client driver is located at:

    • Linux: /usr/local/taos/driver/libtaos.so
    • Windows: C:\TDengine\taos.dll
    • macOS:/usr/local/lib/libtaos.dylib
    • Windows、Linux、MacOS

    • PHP >= 7.4

    • TDengine >= 2.0

    • Swoole >= 4.8 (Optional)

    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.

    Install php-tdengine

    Download Source Code Package and Unzip:

    Non-Swoole Environment:

    1. phpize && ./configure && make -j && make install

    Specify TDengine location:

    Swoole Environment:

    1. 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.

    Establish Connection

    view source code

    Insert Data

    Insert Data

    1. <?php
    2. use TDengine\Connection;
    3. use TDengine\Exception\TDengineException;
    4. try {
    5. // instantiate
    6. $host = 'localhost';
    7. $port = 6030;
    8. $username = 'root';
    9. $password = 'taosdata';
    10. $connection = new Connection($host, $port, $username, $password, $dbname);
    11. // connect
    12. $connection->connect();
    13. // insert
    14. $connection->query('CREATE DATABASE if not exists power');
    15. $connection->query('CREATE STABLE if not exists meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)');
    16. $resource = $connection->query(<<<'SQL'
    17. power.d1002 USING power.meters TAGS(California.SanFrancisco, 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000)
    18. power.d1003 USING power.meters TAGS(California.LosAngeles, 2) VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000) ('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000)
    19. power.d1004 USING power.meters TAGS(California.LosAngeles, 3) VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) ('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000)
    20. SQL);
    21. // get affected rows
    22. var_dump($resource->affectedRows());
    23. } catch (TDengineException $e) {
    24. // throw exception
    25. throw $e;
    26. }

    view source code

    Synchronous Query

    view source code

    Parameter Binding

    Parameter Binding

    1. <?php
    2. use TDengine\Connection;
    3. use TDengine\Exception\TDengineException;
    4. try {
    5. // instantiate
    6. $host = 'localhost';
    7. $port = 6030;
    8. $username = 'root';
    9. $password = 'taosdata';
    10. $dbname = 'power';
    11. $connection = new Connection($host, $port, $username, $password, $dbname);
    12. // connect
    13. $connection->connect();
    14. // insert
    15. $connection->query('CREATE DATABASE if not exists power');
    16. $connection->query('CREATE STABLE if not exists meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)');
    17. $stmt = $connection->prepare('INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)');
    18. // set table name and tags
    19. $stmt->setTableNameTags('d1001', [
    20. // same format as parameter binding
    21. [TDengine\TSDB_DATA_TYPE_INT, 2],
    22. ]);
    23. $stmt->bindParams([
    24. [TDengine\TSDB_DATA_TYPE_TIMESTAMP, 1648432611249],
    25. [TDengine\TSDB_DATA_TYPE_FLOAT, 10.3],
    26. [TDengine\TSDB_DATA_TYPE_INT, 219],
    27. [TDengine\TSDB_DATA_TYPE_FLOAT, 0.31],
    28. ]);
    29. $stmt->bindParams([
    30. [TDengine\TSDB_DATA_TYPE_TIMESTAMP, 1648432611749],
    31. [TDengine\TSDB_DATA_TYPE_FLOAT, 12.6],
    32. [TDengine\TSDB_DATA_TYPE_INT, 218],
    33. [TDengine\TSDB_DATA_TYPE_FLOAT, 0.33],
    34. ]);
    35. $resource = $stmt->execute();
    36. // get affected rows
    37. var_dump($resource->affectedRows());
    38. } catch (TDengineException $e) {
    39. // throw exception
    40. throw $e;

    view source code