forked from couchbaselabs/devguide-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-placeholders.php
30 lines (25 loc) · 1.05 KB
/
query-placeholders.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/**
* Query all airports for a given city
* @param \Couchbase\Bucket $bkt bucket to use
* @param string $city City to search for
* @return array Rows for each city
*/
function query_city($bkt, $city) {
$query = \Couchbase\N1qlQuery::fromString('SELECT airportname FROM `travel-sample` WHERE city=$1 AND type="airport"');
$query->options['args'] = array($city);
// The following optimizes the query for repeated invocation. The query string
// (i.e. the one in fromString) is converted to an optimized form at the
// server and returned to the SDK. Internally the SDK will re-use this optimized form
// for future queries if the adhoc flag is set to false
$query->adhoc(false);
return $bkt->query($query);
}
$cluster = new \Couchbase\Cluster('couchbase://localhost');
$bucket = $cluster->openBucket('travel-sample');
echo "Airports in Reno:\n";
var_dump(query_city($bucket, "Reno"));
echo "Airports in Dallas:\n";
var_dump(query_city($bucket, "Dallas"));
echo "Aiports in Los Angeles\n";
var_dump(query_city($bucket, "Los Angeles"));