influxdata / influxdb-php

influxdb-php: A PHP Client for InfluxDB, a time series database
http://influxdb.com
MIT License
432 stars 125 forks source link

[Feature request] Parameter Binding #140

Open mehmetali opened 4 years ago

mehmetali commented 4 years ago

InfluxDB supports binding parameters. This feature covers issue #53

andres11362 commented 3 years ago

Hi, could you please give me an example of how to use the binding parameters in the library? I'm not sure how to do it.

Sorry for my bad English.

Thanks in advance

lyrixx commented 2 years ago

Here is an example:

require __DIR__ . '/vendor/autoload.php';

use InfluxDB\Database;
use InfluxDB\Point;

$client = new InfluxDB\Client('127.0.0.1');

$database = $client->selectDB('stat');

$points = array(
    new Point(
        'test_metric', // name of the measurement
        0.64, // the measurement value
        ['host' => 'server01', 'region' => 'us-west'], // optional tags
        ['cpucount' => 10], // optional additional fields
        1435255849 // Time precision has to be set to seconds!
    ),
    new Point(
        'test_metric', // name of the measurement
        0.84, // the measurement value
        ['host' => 'server02', 'region' => 'us-west'], // optional tags
        ['cpucount' => 10], // optional additional fields
        1435255849 // Time precision has to be set to seconds!
    )
);

// we are writing unix timestamps, which have a second precision
$result = $database->writePoints($points, Database::PRECISION_SECONDS);

$result = $database->query(
    // 'select * from test_metric where host = $host LIMIT 5',
    'select * from test_metric where "host" = $host LIMIT 5',
    [
            'params' => json_encode([
                'host' => 'server02'
            ]),
    ],
)->getPoints();

dd($result);