Influxql query not working in php8.1 and influxdb2.x

I’m trying to query data using influxql from influxdb 2.7 in php8.1 …while using flux query it works but when I use influxql query it throws error

I have also done dbrp mapping

$client = new Client([
“url” => “http://localhost:8086”,
“token” => $token,
“bucket” => $bucket,
“orgID” => $orgID,
“org”=> $org,
“precision” => InfluxDB2\Model\WritePrecision::S
]);

Php influxql

$queryApi = $client->createQueryApi();

$queryQL = ‘SELECT co FROM "Test"."autogen"."airSensors" WHERE time > now()-3d’;

$result = $queryApi->query($queryQL);

header(‘Content-type:application/json;charset=utf-8’);
//echo json_encode( $result, JSON_PRETTY_PRINT ) ;

$client->close();

On execution throws following error

@Dhaarini What’s the error it throws? What’s your query?

Hello scott, Thanks for your response

My query in php

$queryApi = $client->createQueryApi();

$queryQL = ‘SELECT co FROM “Test”.“autogen”.“airSensors” WHERE time > now()-3d’;

$result = $queryApi->query($queryQL);

"PHP Fatal error: Uncaught InfluxDB2\ApiException: [400] Error connecting to the API (http://localhost:8086/api/v2/query?org=xxx)(compilation failed: error @1:16-1:17: invalid statement: \

error @1:17-1:73: got unexpected token in string expression @1:73-1:73: EOF) in /var/www/html/Test/vendor/influxdata/influxdb-client-php/src/InfluxDB2/DefaultApi.php:164
Stack trace:
#0 /var/www/html/Test/vendor/influxdata/influxdb-client-php/src/InfluxDB2/DefaultApi.php(221): InfluxDB2\DefaultApi->sendRequest()
#1 /var/www/html/Test/vendor/influxdata/influxdb-client-php/src/InfluxDB2/DefaultApi.php(79): InfluxDB2\DefaultApi->request()
#2 /var/www/html/Test/vendor/influxdata/influxdb-client-php/src/InfluxDB2/QueryApi.php(115): InfluxDB2\DefaultApi->post()
#3 /var/www/html/Test/vendor/influxdata/influxdb-client-php/src/InfluxDB2/QueryApi.php(67): InfluxDB2\QueryApi->postQuery()
#4 /var/www/html/Test/Test_ql.php(69): InfluxDB2\QueryApi->query()
#5 {main}
thrown in /var/www/html/Test/vendor/influxdata/influxdb-client-php/src/InfluxDB2/DefaultApi.php on line 164"

But this works fine with curl (influxql) and flux

$curl --get http://localhost:8086/query?db=Test
–header “Authorization: ”
–data-urlencode “q=SELECT co FROM "Test"."autogen"."airSensors" WHERE time > now()-3d”

$queryFlux = ‘from(bucket: “Test”)
|> range(start: -3d)
|> filter(fn: (r) => r[“_measurement”] == “airSensors”)
|> filter(fn: (r) => r[“_field”] == “co”)’;

It appears to be a parsing issue in PHP. Try using double quotes for the string boundaries and then escape the double quotes inside the string:

$queryApi = $client->createQueryApi();

$queryQL = "SELECT co FROM \"Test\".\"autogen\".\"airSensors\" WHERE time > now()-3d";

$result = $queryApi->query($queryQL);

@scott tried with $queryQL = "SELECT co FROM \"Test\".\"autogen\".\"airSensors\" WHERE time > now()-3d"

still getting error

FYI Support for InfluxQL (1.X style queries) · Issue #138 · influxdata/influxdb-client-php · GitHub

Ah, alright. I was thinking the InfluxDB v2 PHP client supported InfluxQL, but it doesn’t. It only supports Flux. You need to use the InfluxDB v1 PHP client. InfluxDB v2 includes a v1 compatibility API, so the v1 PHP client should work with InfluxDB v2.

1 Like

@scott Thanks, could you share some examples of influxDBv1 PHP to connect with influxdb v2 and querying using influxQl

@scott Thanks :+1: , its working now

I have installed [InfluxDB v1 PHP client], using below php code connected to influxdbV2

<?php
$host = 'localhost';
$port = 8086;
$dbname = 'Test';
$username = 'xxxxx';
$password = 'xxxxx';
require __DIR__ . '/vendor/autoload.php';
$client = new InfluxDB\Client($host, $port, $username, $password);
print_r($client);
$database = $client->selectDB($dbname);
$result = $database->query('select co from airSensors WHERE time > now()-4d');
$points = $result->getPoints();
print_r($points);
//header('Content-type:application/json;charset=utf-8');
//echo json_encode( $result, JSON_PRETTY_PRINT ) ;
?>`Preformatted text`

I have created influx v1 auth influx v1 auth create | InfluxDB OSS v2 Documentation