Main Page » Query Mode
Query Mode
The query mode of the Clients allows you to bind external variables to a query and evaluate the query iteratively. The query() function of the Session instance returns a new query instance.
Usage
The query execution works as follows:
- Create a new session instance with hostname, port, username, and password.
- Call
query()with your XQuery expression to get a query object. - Optionally bind variables to the query with one of the
bind()functions. - Optionally bind a value to the context item via
context(). - Iterate through the query object with the
more()andnext()functions. - As an alternative, call
execute()to get the whole result at a time. info()gives you information about query evaluation.options()returns the query serialization parameters.- Don’t forget to close the query with
close().
PHP Example
Taken from our repository:
<?php
/*
* This example shows how external variables can be bound to XQuery expressions.
*/
include_once 'load.php';
use BaseXClient\BaseXException;
use BaseXClient\Session;
try {
// create session
$session = new Session("localhost", 1984, "admin", "...");
try {
// create query instance
$input = 'declare variable $name external; for $i in 1 to 10 return element { $name } { $i }';
$query = $session->query($input);
// bind variable
$query->bind("name", "number");
// print results
print $query->execute()."\n";
// close query instance
$query->close();
} catch (BaseXException $e) {
// print exception
print $e->getMessage();
}
// close session
$session->close();
} catch (BaseXException $e) {
// print exception
print $e->getMessage();
}
Changelog
Version 7.2- Added:
context()function
⚡Generated with XQuery