Changes

Jump to navigation Jump to search
207 bytes added ,  15:53, 17 January 2016
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>
<h2>==Usage</h2> ==
<p>The query execution works as follows:</p>
<ol start="1"> <li># Create a new session instance with hostname, port, username and password.</li> <li># Call the <code>query()</code> function of the session with the query as argument your XQuery expression to get your a query object.</li> <li># Optionally bind variables to the query with one of the <code>bind()</code> functionfunctions.</li> <li>Initialize query output # Optionally bind a value to the context item via <code>initcontext()</code>.</li> <li># 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()</licode> gives you information on query evaluation.# <code>options()<li/code>Close returns the query serialization parameters.# 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]: </lipre class="brush:php"> <?php/ol> * * This example shows how queries can be executed in an iterative manner.<h2>Example< * Documentation: http://basex.org/h2> api * * (C) BaseX Team 2005-15, BSD License */<ol start=include("1BaseXClient.php"> ); try {<li>Create a // create session object: <code>Session $session = new Session("localhost", 1984, "admin", "admin");< try { /code></li> <li>Define a query and create a query object:<br/> instance <code>String q $input = "'declare variable $name external; " +'. " 'for $i in 1 to 10 return element { $name } { $i }"';Query $query = $session.->query(q$input);<  /code></li> <li>Call the bind method of your query object:<br/> variable <code$query->query.bind("$name", "Numbernumber");<  /code></li> <li>Call the init method of your query object:<br/> print result <code>print $query.init->execute()."\n";<  /code></li> <li>Iterate through the close query object:<br/> instance <code$query->whileclose(query.more);  } catch (Exception $e)) { // print exception print query.next$e->getMessage();< }  /code></liclose session $session-> close(); } catch (Exception $e) {<li>Close your query object:<br //> print exception <code print $e->print query.closegetMessage();</code}?></lipre> </ol>=Changelog= ;Version 7.2 * Added: {{Code|context()}} function
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu