By default, parameters MUST be bound using named or positional bindings.
Literals are disabled by default, and must be enabled by setting$options['allowLiterals']totrue. As with any SQL dialect, using
parameter binding is highly recommended.
use Google\Cloud\Datastore\DatastoreClient;
$datastore = new DatastoreClient();
$query = $datastore->gqlQuery('SELECT * FROM Companies');
$res = $datastore->runQuery($query);
foreach ($res as $company) {
echo $company['companyName'] . PHP_EOL;
}
// Literals must be provided as bound parameters by default:
$query = $datastore->gqlQuery('SELECT * FROM Companies WHERE companyName = @companyName', [
'bindings' => [
'companyName' => 'Google'
]
]);
// Positional binding is also supported:
$query = $datastore->gqlQuery('SELECT * FROM Companies WHERE companyName = @1 LIMIT 1', [
'bindings' => [
'Google'
]
]);
// While not recommended, you can use literals in your query string:
$query = $datastore->gqlQuery('SELECT * FROM Companies WHERE companyName = \'Google\'', [
'allowLiterals' => true
]);
// Using cursors as query bindings:
$cursor = $datastore->cursor($cursorValue);
$query = $datastore->gqlQuery('SELECT * FROM Companies OFFSET @offset', [
'bindings' => [
'offset' => $cursor
]
]);
Whether literal values will be allowed in the query string. Parameter binding is strongly encouraged over literals.Defaults tofalse.
↳ bindings
array
An array of values to bind to the query string. Queries using Named Bindings should provide a key/value set, while queries using Positional Bindings must provide a simple array. Applications with no need for multitenancy should not set this value.
queryObject
Format the query for use in the API.
Returns
Type
Description
array
queryKey
Return the query_type union field name.
Returns
Type
Description
string
aggregation
canPaginate
Indicate that this type does not support automatic pagination.
Returns
Type
Description
bool
start
Fulfill the interface, but cursors are handled inside the query string.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Datastore Client - Class GqlQuery (1.33.1)\n\nVersion latestkeyboard_arrow_down\n\n- [1.33.1 (latest)](/php/docs/reference/cloud-datastore/latest/Query.GqlQuery)\n- [1.33.0](/php/docs/reference/cloud-datastore/1.33.0/Query.GqlQuery)\n- [1.32.3](/php/docs/reference/cloud-datastore/1.32.3/Query.GqlQuery)\n- [1.31.0](/php/docs/reference/cloud-datastore/1.31.0/Query.GqlQuery)\n- [1.30.0](/php/docs/reference/cloud-datastore/1.30.0/Query.GqlQuery)\n- [1.29.2](/php/docs/reference/cloud-datastore/1.29.2/Query.GqlQuery)\n- [1.28.2](/php/docs/reference/cloud-datastore/1.28.2/Query.GqlQuery)\n- [1.26.0](/php/docs/reference/cloud-datastore/1.26.0/Query.GqlQuery)\n- [1.25.0](/php/docs/reference/cloud-datastore/1.25.0/Query.GqlQuery)\n- [1.24.4](/php/docs/reference/cloud-datastore/1.24.4/Query.GqlQuery)\n- [1.23.0](/php/docs/reference/cloud-datastore/1.23.0/Query.GqlQuery)\n- [1.22.1](/php/docs/reference/cloud-datastore/1.22.1/Query.GqlQuery)\n- [1.21.2](/php/docs/reference/cloud-datastore/1.21.2/Query.GqlQuery)\n- [1.19.0](/php/docs/reference/cloud-datastore/1.19.0/Query.GqlQuery)\n- [1.18.1](/php/docs/reference/cloud-datastore/1.18.1/Query.GqlQuery)\n- [1.17.1](/php/docs/reference/cloud-datastore/1.17.1/Query.GqlQuery) \nReference documentation and code samples for the Cloud Datastore Client class GqlQuery.\n\nQuery Google Cloud Datastore using [GQL](https://cloud.google.com/datastore/docs/apis/gql/gql_reference).\n\nBy default, parameters MUST be bound using named or positional bindings.\nLiterals are disabled by default, and must be enabled by setting\n`$options['allowLiterals']` to `true`. As with any SQL dialect, using\nparameter binding is highly recommended.\n\nIdiomatic usage is via [Google\\\\Cloud\\\\Datastore\\\\DatastoreClient::gqlQuery()](/php/docs/reference/cloud-datastore/latest/DatastoreClient#_Google_Cloud_Datastore_DatastoreClient__gqlQuery__).\n\nExample: \n\n use Google\\Cloud\\Datastore\\DatastoreClient;\n\n $datastore = new DatastoreClient();\n\n $query = $datastore-\u003egqlQuery('SELECT * FROM Companies');\n $res = $datastore-\u003erunQuery($query);\n\n foreach ($res as $company) {\n echo $company['companyName'] . PHP_EOL;\n }\n\n // Literals must be provided as bound parameters by default:\n $query = $datastore-\u003egqlQuery('SELECT * FROM Companies WHERE companyName = @companyName', [\n 'bindings' =\u003e [\n 'companyName' =\u003e 'Google'\n ]\n ]);\n\n // Positional binding is also supported:\n $query = $datastore-\u003egqlQuery('SELECT * FROM Companies WHERE companyName = @1 LIMIT 1', [\n 'bindings' =\u003e [\n 'Google'\n ]\n ]);\n\n // While not recommended, you can use literals in your query string:\n $query = $datastore-\u003egqlQuery('SELECT * FROM Companies WHERE companyName = \\'Google\\'', [\n 'allowLiterals' =\u003e true\n ]);\n\n // Using cursors as query bindings:\n $cursor = $datastore-\u003ecursor($cursorValue);\n\n $query = $datastore-\u003egqlQuery('SELECT * FROM Companies OFFSET @offset', [\n 'bindings' =\u003e [\n 'offset' =\u003e $cursor\n ]\n ]);\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Datastore \\\\ Query\n\nMethods\n-------\n\n### __construct\n\n### queryObject\n\nFormat the query for use in the API.\n\n### queryKey\n\nReturn the query_type union field name.\n\n### aggregation\n\n### canPaginate\n\nIndicate that this type does not support automatic pagination.\n\n### start\n\nFulfill the interface, but cursors are handled inside the query string.\n\n### jsonSerialize\n\nDefine the json representation of the object.\n\nConstants\n---------\n\n### BINDING_NAMED\n\n Value: 'namedBindings'\n\n### BINDING_POSITIONAL\n\n Value: 'positionalBindings'"]]