V3.0.2 Invalid State Exception with basic N1QL Query

I pulled a short code sample from this page:
https://docs.couchbase.com/php-sdk/current/project-docs/migrating-sdk-code-to-3.n.html

$cluster->query is throwing InvalidStateException with an empty message and code 214.

// Version 3.0.2
$connectionString = "couchbase://localhost";
$options = new \Couchbase\ClusterOptions();
$options->credentials("admin", "password");
$cluster = new \Couchbase\Cluster($connectionString, $options);

// This works successfully
//
//$bucket = $cluster->bucket("test-ing");
//$collection = $bucket->defaultCollection();
//$collection->upsert('test-document', ["name"=>"test-document"]);
//$res = $collection->get('test-document');

$options = new \Couchbase\QueryOptions();
$options->namedParameters(['city' => "Los Angeles", 'type' => "airport"]);

// This line throws
$result = $cluster->query('SELECT airportname FROM `travel-sample` WHERE city=$city AND type=$airport', $options);
foreach ($result->rows() as $row) {
    printf("%s\n", $row['airportname']);
}

Here’s the thrown exception:

^ Couchbase\InvalidStateException {#774 ▼
  #message: ""
  #code: 214
  #file: "/.../routes/web.php"
  #line: 226
  #ref: null
  #context: null
  #is_input: null
  #is_network: null
  #is_fatal: null
  #is_transient: null
  #is_data_operation: null
  #is_internal: null
  #is_plugin: null
  #is_server_under_load: null
  #is_server_generated: null
  #is_subdoc: null
  #is_durability: null
  trace: {▼
    /.../routes/web.php:226 {▼
      Illuminate\Routing\RouteFileRegistrar->{closure} …
      › $options->namedParameters(['city' => "Los Angeles", 'type' => "airport"]);
      › $result = $cluster->query('SELECT airportname FROM `travel-sample` WHERE city=$city AND type=$airport', $options);
      › foreach ($result->rows() as $row) {
      arguments: {▼
        $statement: "SELECT airportname FROM `travel-sample` WHERE city=$city AND type=$airport"
        $queryOptions: Couchbase\QueryOptions {#775 …}
      }
    }

What is your server version? Could you capture logs (set INI setting couchbase.log_level to "DEBUG" or "INFO")?

Server is Community v 6.0.0 Build 1693

Logs:

[17-Mar-2020 20:41:21 UTC] [cb,DEBG] (pcbc/pool L:356) cachehit: type=1, connstr=couchbase://localhost, bucketname=(null), username=admin, lcb=0x7ffe5d46b820, refs=5. I=0x7ffe5d46b820
[17-Mar-2020 20:41:21 UTC] [cb,DEBG] (pcbc/cluster L:98) Initialize Cluster. C=0x10a32a9b0 connstr="couchbase://localhost"
[17-Mar-2020 20:41:21 UTC] [cb,DEBG] (pcbc/pool L:356) cachehit: type=1, connstr=couchbase://localhost, bucketname=(null), username=admin, lcb=0x7ffe5d46b820, refs=6. I=0x7ffe5d46b820
[17-Mar-2020 20:41:21 UTC] [cb,DEBG] (pcbc/cluster L:98) Initialize Cluster. C=0x10a32a960 connstr="couchbase://localhost"

CB Client Info is:

---- PHP Info ----
PHP:7.4.3

---- Couchbase Client Info ----
couchbase

couchbase support	enabled
extension version	3.0.2
libcouchbase runtime version	3.0.0_18_gc3179958d4 (git: c3179958d4db71ee70243a1f8470edbf9e405f08)
libcouchbase headers version	3.0.0_18_gc3179958d4 (git: c3179958d4db71ee70243a1f8470edbf9e405f08)
zlib compressor	enabled

Directive	Local Value	Master Value
couchbase.log_level	DEBUG	DEBUG
couchbase.encoder.format	json	json
couchbase.encoder.compression	off	off
couchbase.encoder.compression_threshold	0	0
couchbase.encoder.compression_factor	0.0	0.0
couchbase.decoder.json_arrays	0	0
couchbase.pool.max_idle_time_sec	60	60

@jrlawhorne note sure if this helps but following the docs and the code snippet you have I see that a tick symbol is missing in your parameterized select query

$statement: "SELECT airportname FROM `travel-sample` WHERE `city`=$city AND `type`=$airport"

can you add the ticks to the predicates / filters and try please ?

That doesn’t appear to be the problem. This line also throws the same exception:

$result = $cluster->query('SELECT 1=1');

Anybody got any solution? I am stuck for days now. Here is my config details for help-
-PHP 7.4.10
-Community Edition 5.1.1 build 5723
-PHP SDK 3

FYI, I am getting this error after upgrading the SDK from 2.6. And now I can’t even downgrade, because installing SDK 2.6 giving me another error. Totally stuck here.

Hi @comet. The issue was that PHP SDK hasn’t received the configuration from the server (5.1.1 does not support new bootstrap protocol yet).

I have fixed the issue on master (scheduled to next release 3.0.4 that going to be published in October).

The fix adds new INI setting couchbase.allow_fallback_to_bucket_connection, and if it set to true, the library will use old bootstrap mechanism will be used for Cluster object when the connection string has bucket name in it.

php -d couchbase.allow_fallback_to_bucket_connection=1 test.php
<?php
$options = new \Couchbase\ClusterOptions();
$options->credentials('Administrator', 'password');
// note "default" bucket name in the connection string
$cluster = new \Couchbase\Cluster('couchbase://localhost/default', $options);
$res = $cluster->query('SELECT 1 = 1');
var_dump($res);

Thanks @avsej. I had to upgrade to 6.5 . Now its working.