File: /src/QueryPath/Extension/QPDB.php

Description

This package contains classes for handling database transactions from within QueryPath.

The tools here use the PDO (PHP Data Objects) library to execute database functions.

Using tools in this package, you can write QueryPath database queries that query an RDBMS and then insert the results into the document.

Example:

  1.  <?php
  2.  $template '<?xml version="1.0"?><tr><td class="colOne"/><td class="colTwo"/><td class="colThree"/></tr>';
  3.  $qp qp(QueryPath::HTML_STUB'body'// Open a stub HTML doc and select <body/>
  4.   ->append('<table><tbody/></table>')
  5.   ->dbInit($this->dsn)
  6.   ->queryInto('SELECT * FROM qpdb_test WHERE 1'array()$template)
  7.   ->doneWithQuery()
  8.   ->writeHTML();
  9.  ?>

The code above will take the results of a SQL query and insert them into a n HTML table.

If you are doing many database operations across multiple QueryPath objects, it is better to avoid using QPDB::dbInit(). Instead, you should call the static QPDB::baseDB() method to configure a single database connection that can be shared by all QueryPath instances.

Thus, we could rewrite the above to look like this:

  1.  <?php
  2.  QPDB::baseDB($someDN);
  3.  
  4.  $template '<?xml version="1.0"?><tr><td class="colOne"/><td class="colTwo"/><td class="colThree"/></tr>';
  5.  $qp qp(QueryPath::HTML_STUB'body'// Open a stub HTML doc and select <body/>
  6.   ->append('<table><tbody/></table>')
  7.   ->queryInto('SELECT * FROM qpdb_test WHERE 1'array()$template)
  8.   ->doneWithQuery()
  9.   ->writeHTML();
  10.  ?>
Note that in this case, the QueryPath object doesn't need to call a method to activate the database. There is no call to dbInit(). Instead, it checks the base class to find the shared database.

(Note that if you were to add a dbInit() call to the above, it would create a new database connection.)

The result of both of these examples will be identical. The output looks something like this:

  1.  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  <html xmlns="http://www.w3.org/1999/xhtml">
  3.  <head>
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
  5.      <title>Untitled</title>
  6.  </head>
  7. <body>
  8. <table>
  9.  <tbody>
  10.   <tr>
  11.    <td class="colOne">Title 0</td>
  12.    <td class="colTwo">Body 0</td>
  13.    <td class="colThree">Footer 0</td>
  14.   </tr>
  15.   <tr>
  16.    <td class="colOne">Title 1</td>
  17.    <td class="colTwo">Body 1</td>
  18.    <td class="colThree">Footer 1</td>
  19.   </tr>
  20.   <tr>
  21.    <td class="colOne">Title 2</td>
  22.    <td class="colTwo">Body 2</td>
  23.    <td class="colThree">Footer 2</td>
  24.   </tr>
  25.   <tr>
  26.    <td class="colOne">Title 3</td>
  27.    <td class="colTwo">Body 3</td>
  28.    <td class="colThree">Footer 3</td>
  29.   </tr>
  30.   <tr>
  31.    <td class="colOne">Title 4</td>
  32.    <td class="colTwo">Body 4</td>
  33.    <td class="colThree">Footer 4</td>
  34.   </tr>
  35.  </tbody>
  36. </table>
  37. </body>
  38. </html>

Note how the CSS classes are used to correlate DB table names to template locations.

Classes defined in this file

CLASS NAME

DESCRIPTION

QPDB Provide DB access to a QueryPath object.

Include/Require Statements

Global Variables

Constants

Functions


Documentation generated on Sun, 25 Jul 2010 16:09:05 -0500 by phpDocumentor 1.4.3