【问题标题】:Couchbase N1QL's select statement with named parameters in PHPCouchbase N1QL 在 PHP 中使用命名参数的 select 语句
【发布时间】:2016-03-25 00:07:13
【问题描述】:

我试图在 PHP 中使用 N1QL...第一个正在工作: 1)

$query = CouchbaseN1qlQuery::fromString("select count(*) from mybucket where type = 'user'");
$res = $myBucket->query($query);

输出:

array(1) {
  [0]=>
  object(stdClass)#3 (1) {
    ["$1"]=>
    int(58)
  }
}

2) 当我更改为命名参数时,它失败了:

$query = CouchbaseN1qlQuery::fromString("select count(*) from mybucket where type = $type");
$res = $myBucket->query($query, ['type' => 'user']);

我收到“PHP 通知:未定义变量:类型...”错误

谁能帮助指导我在 PHP 中为 N1QL 使用命名参数的正确语法?

我的 couchbase 服务器是 4.1

【问题讨论】:

    标签: php couchbase n1ql


    【解决方案1】:

    看来您走在正确的轨道上。问题是您在 PHP 字符串中使用了 $type,这导致 PHP 处理器尝试将其替换为名为 type 的变量。您应该将 N1QL 字符串用单引号而不是双引号括起来,以避免 PHP 解释器尝试进行这种字符串处理。

    Couchbase PHP SDK 还提供 API 用于设置命名参数:

    $query = CouchbaseN1qlQuery::fromString('SELECT airportname FROM `travel-sample` WHERE city=$city AND type=$airport');
    $query->namedParams(['city' => "Los Angeles", 'type' => "airport"]);
    

    这是提供命名参数的首选方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      相关资源
      最近更新 更多