Query Mode
The query mode of the Clients allows you to bind external variables to a query
and evaluate the query in an iterative manner. 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 the
query()
function of the session with the query as argument to get your query object. - Optionally bind variables to the query with the
bind()
function. - Iterate through the query object with the
more()
andnext()
functions. If an error occurs, an exception is thrown. - As an alternative, call
execute()
to get the whole result at a time. This will be faster in most cases, as the socket communication will be limited to a single call. info()
gives you information on query evaluation.options()
returns the query serialization parameters.- Close the query with
close()
.
PHP Example
Taken from our repository:
<?php /* * This example shows how queries can be executed in an iterative manner. * Documentation: http://basex.org/api * * (C) BaseX Team 2005-11, BSD License */ include("BaseXClient.php"); try { // create session $session = new Session("localhost", 1984, "admin", "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 result print $query->execute()."\n"; // close query instance $query->close(); } catch (Exception $e) { // print exception print $e->getMessage(); } // close session $session->close(); } catch (Exception $e) { // print exception print $e->getMessage(); } ?>