2022-05-21 03:11:57 +00:00
< ? php
use TDengine\Connection ;
use TDengine\Exception\TDengineException ;
try {
2022-05-27 05:26:28 +00:00
// instantiate
2022-05-21 03:11:57 +00:00
$host = 'localhost' ;
$port = 6030 ;
$username = 'root' ;
$password = 'taosdata' ;
$dbname = 'power' ;
$connection = new Connection ( $host , $port , $username , $password , $dbname );
2022-05-27 05:26:28 +00:00
// connect
2022-05-21 03:11:57 +00:00
$connection -> connect ();
2022-05-27 05:26:28 +00:00
// insert
2022-05-21 03:11:57 +00:00
$connection -> query ( 'CREATE DATABASE if not exists power' );
$connection -> query ( 'CREATE STABLE if not exists meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)' );
$stmt = $connection -> prepare ( 'INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)' );
2022-05-27 05:26:28 +00:00
// set table name and tags
2022-05-21 03:11:57 +00:00
$stmt -> setTableNameTags ( 'd1001' , [
2022-06-07 02:36:32 +00:00
// same format as parameter binding
2022-05-27 05:26:28 +00:00
[ TDengine\TSDB_DATA_TYPE_BINARY , 'California.SanFrancisco' ],
2022-05-21 03:11:57 +00:00
[ TDengine\TSDB_DATA_TYPE_INT , 2 ],
]);
$stmt -> bindParams ([
[ TDengine\TSDB_DATA_TYPE_TIMESTAMP , 1648432611249 ],
[ TDengine\TSDB_DATA_TYPE_FLOAT , 10.3 ],
[ TDengine\TSDB_DATA_TYPE_INT , 219 ],
[ TDengine\TSDB_DATA_TYPE_FLOAT , 0.31 ],
]);
$stmt -> bindParams ([
[ TDengine\TSDB_DATA_TYPE_TIMESTAMP , 1648432611749 ],
[ TDengine\TSDB_DATA_TYPE_FLOAT , 12.6 ],
[ TDengine\TSDB_DATA_TYPE_INT , 218 ],
[ TDengine\TSDB_DATA_TYPE_FLOAT , 0.33 ],
]);
$resource = $stmt -> execute ();
2022-05-27 05:26:28 +00:00
// get affected rows
2022-05-21 03:11:57 +00:00
var_dump ( $resource -> affectedRows ());
} catch ( TDengineException $e ) {
2022-05-27 05:26:28 +00:00
// throw exception
2022-05-21 03:11:57 +00:00
throw $e ;
}