Difference between revisions of "Standard Mode"

From BaseX Documentation
Jump to navigation Jump to search
Line 17: Line 17:
 
==Example in [https://svn.uni-konstanz.de/dbis/basex/trunk/api/etc/php/ PHP]==  
 
==Example in [https://svn.uni-konstanz.de/dbis/basex/trunk/api/etc/php/ PHP]==  
 
   
 
   
<pre class="brush:java">
+
<pre class="brush:php">
// Create a session object:
+
<?php
Session session = new Session("localhost", 1984, "admin", "admin");
+
/*
 +
* This example shows how database commands can be executed.
 +
* Documentation: http://basex.org/api
 +
*
 +
* (C) BaseX Team 2005-11, ISC License
 +
*/
 +
include("BaseXClient.php");
  
// Run the command in question and print the textual result:
+
try {
print session.execute("info database");
+
  // initialize timer
 +
  $start = microtime(true);
  
// Close the session:
+
  // create session
session.close();
+
  $session = new Session("localhost", 1984, "admin", "admin");
 +
 
 +
  // perform command and print returned string
 +
  print $session->execute("xquery 1 to 10");
 +
 
 +
  // close session
 +
  $session->close();
 +
 
 +
  // print time needed
 +
  $time = (microtime(true) - $start) * 1000;
 +
  print "\n$time ms\n";
 +
 
 +
} catch (Exception $e) {
 +
  // print exception
 +
  print $e->getMessage();
 +
}
 +
?>
 
</pre>
 
</pre>
 
[[Category:Developer]]
 
[[Category:Developer]]
 
[[Category:Server]]
 
[[Category:Server]]
 
[[Category:Language Bindings]]
 
[[Category:Language Bindings]]

Revision as of 12:28, 10 January 2011

In the standard mode, a database command can be sent to the server using the execute() function of the Session. This functions returns the whole result. With the info() function, you can request some information on your executed process. If an error occurs, an exception with the error message will be thrown.

Usage

The standard execution works as follows:

  1. Create a new session instance with hostname, port, username and password.
  2. Call the execute() function of the session with the database commands as argument.
  3. Receive the result of a successfully executed command. As an error occurs an exception is thrown.
  4. Continue using the client (back to 2.), or close the session.

Example in PHP

<?php
/*
 * This example shows how database commands can be executed.
 * Documentation: http://basex.org/api
 *
 * (C) BaseX Team 2005-11, ISC License
 */
include("BaseXClient.php");

try {
  // initialize timer
  $start = microtime(true);

  // create session
  $session = new Session("localhost", 1984, "admin", "admin");
  
  // perform command and print returned string
  print $session->execute("xquery 1 to 10");

  // close session
  $session->close();

  // print time needed
  $time = (microtime(true) - $start) * 1000;
  print "\n$time ms\n";

} catch (Exception $e) {
  // print exception
  print $e->getMessage();
}
?>