Changes

Jump to navigation Jump to search
180 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>
==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 in == Taken from our [https://svn.uni-konstanzgithub.decom/dbisBaseXdb/basex-api/trunkblob/apimaster/src/etcmain/php/ PHPQueryBindExample.php repository]== :
<pre class="brush:php">
<?php
/*
* This example shows how database commands queries can be executedin an iterative manner.
* Documentation: http://basex.org/api
*
* (C) BaseX Team 2005-1115, ISC BSD License
*/
include("BaseXClient.php");
try {
// initialize timer
$start = microtime(true);
 
// 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");  // perform command and print returned stringresult print $sessionquery->execute()."xquery 1 to 10\n";  // close query instance $query->close();  } catch (Exception $e) { // print exception print $e->getMessage(); }
// close session
$session->close();
 
// print time needed
$time = (microtime(true) - $start) * 1000;
print "\n$time ms\n";
} catch (Exception $e) {
</pre>
for more examples see the [[Languages|languages]] section=Changelog= ;Version 7.2[[Category:Developer]][[Category* Added:Server]][[Category:Language Bindings]]{{Code|context()}} function
Bureaucrats, editor, reviewer, Administrators
13,551

edits

Navigation menu