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

**URL:** <https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662>\
**Category:** InfluxDB 2\
**Tags:** influxql, php\
**Created:** [September 24, 2024, 8:04pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662 "2024-09-24T20:04:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhaarini](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dhaarini](https://community.influxdata.com/u/Dhaarini)\
**Post date:** [September 24, 2024, 8:04pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/1 "2024-09-24T20:04:17Z")

</div>

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](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

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/3X/2/9/29376f82e3bc3f555d007409cbb9271c65c212f8.png)

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [September 24, 2024, 8:06pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/2 "2024-09-24T20:06:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dhaarini](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dhaarini](https://community.influxdata.com/u/Dhaarini)\
**Post date:** [September 24, 2024, 8:30pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/3 "2024-09-24T20:30:29Z")

</div>

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](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](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”)’;

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [September 24, 2024, 8:59pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/4 "2024-09-24T20:59:47Z")

</div>

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:

```php
$queryApi = $client->createQueryApi();

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

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

```

---

<div class="post-metadata">

**Author:** ![Dhaarini](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dhaarini](https://community.influxdata.com/u/Dhaarini)\
**Post date:** [September 24, 2024, 9:11pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/5 "2024-09-24T21:11:25Z")

</div>

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

still getting error

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/3X/a/0/a08f5e1d5041e9fa0353dff144f4f695c6f35f48.png)

FYI [Support for InfluxQL (1.X style queries) · Issue #138 · influxdata/influxdb-client-php · GitHub](https://github.com/influxdata/influxdb-client-php/issues/138)

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [September 24, 2024, 9:26pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/6 "2024-09-24T21:26:21Z")

</div>

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](https://github.com/influxdata/influxdb-php). InfluxDB v2 includes a v1 compatibility API, so the v1 PHP client should work with InfluxDB v2.

---

<div class="post-metadata">

**Author:** ![Dhaarini](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dhaarini](https://community.influxdata.com/u/Dhaarini)\
**Post date:** [September 24, 2024, 9:35pm UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/7 "2024-09-24T21:35:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dhaarini](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dhaarini](https://community.influxdata.com/u/Dhaarini)\
**Post date:** [September 25, 2024, 8:20am UTC](https://community.influxdata.com/t/influxql-query-not-working-in-php8-1-and-influxdb2-x/37662/8 "2024-09-25T08:20:51Z")

</div>

@scott Thanks 👍 , its working now

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

```auto
<?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](https://docs.influxdata.com/influxdb/v2/reference/cli/influx/v1/auth/create/)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/3X/f/3/f3dd3c32e702d267f63e0f5524f8c445ffe5d96e.png)
