Difference between revisions of "Query Mode"
Jump to navigation
Jump to search
Line 22: | Line 22: | ||
<?php | <?php | ||
/* | /* | ||
− | * This example shows how | + | * This example shows how queries can be executed in an iterative manner. |
* Documentation: http://basex.org/api | * Documentation: http://basex.org/api | ||
* | * | ||
Line 30: | Line 30: | ||
try { | try { | ||
− | |||
− | |||
− | |||
// create session | // create session | ||
$session = new Session("localhost", 1984, "admin", "admin"); | $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"); | ||
+ | |||
+ | // initialize query | ||
+ | print $query->init(); | ||
+ | |||
+ | // loop through all results | ||
+ | while($query->more()) { | ||
+ | print $query->next()."\n"; | ||
+ | } | ||
+ | |||
+ | // close query instance | ||
+ | print $query->close(); | ||
+ | |||
+ | } catch (Exception $e) { | ||
+ | // print exception | ||
+ | print $e->getMessage(); | ||
+ | } | ||
// close session | // close session | ||
$session->close(); | $session->close(); | ||
− | |||
− | |||
− | |||
− | |||
} catch (Exception $e) { | } catch (Exception $e) { |
Revision as of 13:28, 10 January 2011
In the query mode a query can be send to the server and executed
in an iterative manner. For this you have to call the query()
function
of the Session
with your defined query. This will return a query object
which 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 bind()
function
of the query object.
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. - Initialize query output via
init()
. - Iterate through the query object with the
more()
andnext()
functions. If an error occurs, an exception is thrown. - Close the query with
close()
.
Example in PHP
<?php /* * This example shows how queries can be executed in an iterative manner. * Documentation: http://basex.org/api * * (C) BaseX Team 2005-11, ISC 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"); // initialize query print $query->init(); // loop through all results while($query->more()) { print $query->next()."\n"; } // close query instance print $query->close(); } catch (Exception $e) { // print exception print $e->getMessage(); } // close session $session->close(); } catch (Exception $e) { // print exception print $e->getMessage(); } ?>
for more examples see the languages section.