I am trying to do a SELECT with Couchbase, N1QL and PHP. This will get the whole row. I need to get a certain field, for example country. How do I do that? This is my current script.
// Set bucket
$bucketName = "travel-sample";
// Connect to Couchbase Server
$cluster = new CouchbaseCluster("couchbase://127.0.0.1");
$bucket = $cluster->openBucket($bucketName);
// Get data
$query = CouchbaseN1qlQuery::fromString('SELECT * FROM `travel-sample` LIMIT 4');
$result = $bucket->query($query);
foreach ($result->rows as $row) {
$json = json_encode($row);
echo $json;
echo "<br>";
echo "<br>";
}
Change your SQL statement so that it returns only the desired field(s). Using the "*", you are asking you DB for all the fields in table travel-sample.
Changing your query line to
$query = CouchbaseN1qlQuery::fromString('SELECT country FROM `travel-sample` LIMIT 4');
or (multiple fields)
$query = CouchbaseN1qlQuery::fromString('SELECT country, other_field FROM `travel-sample` LIMIT 4');
Should work for you.