Changes

Jump to navigation Jump to search
413 bytes added ,  14:20, 27 February 2020
no edit summary
<p>In the The query mode of the [[Clients]] allows you to bind external variables to a query can be send to and evaluate the server and executedquery in an iterative manner. For this you have to call the The <code>query()</code> functionof the <code>Session</code> with your defined query. This will return instance returns a new query objectwhich comes with an forward-only iterator to get each result of the query.Furthermore it is possible to bind variables to the query using the <code>bind()</code> functionof the query objectinstance.</p>
==Usage==
<p>The query execution works as follows:</p>
#Create a new session instance with hostname, port, username and password.#Call the <code>query()</code> function of the session with the query as argument your XQuery expression to get your a query object.#Optionally bind variables to the query with one of the <code>bind()</code> functionfunctions.#Initialize query output Optionally bind a value to the context item via <code>initcontext()</code>.#Iterate through the query object with the <code>more()</code> and <code>next()</code> functions. If # As an error occursalternative, an exception is throwncall <code>execute()</code> to get the whole result at a time.# <code>info()</code> gives you information on query evaluation.# <code>options()</code> returns the query serialization parameters.#Close Don't forget to close the query with <code>close()</code>.
==PHP Example==  Taken from our [https://github.com/BaseXdb/basex-api/blob/master/src/main/php/QueryBindExample.php repository]: <syntaxhighlight lang="php"><?php/* * This example shows how queries can be executed in an iterative manner. * Documentation: http://basex.org/api * * (C) BaseX Team 2005-15, 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();
<pre class="brush:java"> } catch (Exception $e) { // Create a session objectprint exceptionSession session = new Session print $e->getMessage("localhost", 1984, "admin", "admin"); }
// Define a query and create a query objectclose sessionString q = "declare variable $name external; for $i in 1 to 10 return element { $name } { $i }";Query query = session.query->close(q);
} catch (Exception $e) { // Call the bind method of your query objectprint exceptionquery.bind print $e->getMessage("$name", "Number");}?></syntaxhighlight>
// Call the init method of your query objectprint query.init();=Changelog=
// Iterate through the query objectwhile(query;Version 7.more()) print query.next();2
// Close your query object* Added:print query.close{{Code|context();</pre>[[Category:Developer]][[Category:Server]][[Category:Language Bindings]]}} function
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu