Located in File: /src/QueryPath/Extension/QPDB.php
This extension provides tools for communicating with a database using the QueryPath library. It relies upon PDO for underlying database communiction. This means that it supports all databases that PDO supports, including MySQL, PostgreSQL, and SQLite.
Here is an extended example taken from the unit tests for this library.
Let's say we create a database with code like this:
From QueryPath with QPDB, we can now do very elaborate DB chains like this:
When the last command, QueryPath:::writeHTML(), is run, we will get output like this:
Sometimes you want to do something a lot simpler, like give QueryPath a template and have it navigate a query, inserting the data into a template, and then inserting the template into the document. This can be done simply with the queryInto() function.
Here's an example from another unit test:
Typical starting methods for this class are QPDB::baseDB(), QPDB::query(), and QPDB::queryInto().
$cycleRows = FALSE (line 299)
Static Method baseDB (line 272)
This method need be called only once. From there, other QPDB instances will (by default) share the same database instance.
Normally, a DSN should be passed in. Username, password, and db params are all passed in using the options array.
On rare occasions, it may be more fitting to pass in an existing database connection (which must be a PDO object). In such cases, the $dsn parameter can take a PDO object instead of a DSN string. The standard options will be ignored, though.
Warning: If you pass in a PDO object that is configured to NOT throw exceptions, you will need to handle error checking differently.
Remember to always use QPDB::doneWithQuery() when you are done with a query. It gives PDO a chance to clean up open connections that may prevent other instances from accessing or modifying data.
Static Method getBaseDB (line 294)
Constructor __construct (line 304)
Method addData (line 559)
Method appendColumn (line 634)
This appends data to every item in the current QueryPath. The data will be retrieved from the database result, using $columnName as the key.
Method columnAfter (line 692)
This inserts data from the given columns after each element in the QueryPath object. IF HTML/XML is given in the $wrap parameter, then the column data will be wrapped in that markup before being inserted into the QueryPath.
Method columnBefore (line 671)
This inserts the data before each element in the currently matched QueryPath.
Method dbInit (line 343)
This creates a database connection that will last for the duration of the QueryPath object. This method ought to be used only in two cases:
Remember to always use QPDB::doneWithQuery() when you are done with a query. It gives PDO a chance to clean up open connections that may prevent other instances from accessing or modifying data.
Method doneWithQuery (line 477)
This function should always be called when the database results for a query are no longer needed. This frees up the database cursor, discards the data, and resets resources for future use.
If this method is not called, some PDO database drivers will not allow subsequent queries, while others will keep tables in a locked state where writes will not be allowed.
Method exec (line 502)
If your SQL query will have parameters, you are encouraged to use query(), which includes built-in SQL Injection protection.
Method getLastInsertID (line 614)
This will only return a meaningful result when used after an INSERT.
Method getStatement (line 599)
Method nextRow (line 523)
In a result set where more than one row was returned, this will move the pointer to the next row in the set.
The PDO library does not have a consistent way of determining how many rows a result set has. The suggested technique is to first execute a COUNT() SQL query and get the data from that.
The withEachRow() method will begin at the next row after the currently selected one.
Method prependColumn (line 652)
This takes the data from the given column(s) and inserts it into each element currently found in the QueryPath.
Method query (line 405)
This will execute a SQL query (as a prepared statement), and then store the results internally for later use. The data can be iterated using nextRow(). QueryPath can also be instructed to do internal iteration using the withEachRow() method. Finally, on the occasion that the statement itself is needed, getStatement() can be used.
Use this when you need to access the results of a query, or when the parameter to a query should be escaped. If the query takes no external parameters and does not return results, you may wish to use the (ever so slightly faster) exec() function instead.
Make sure you use doneWithQuery() after finishing with the database results returned by this method.
Usage
Here is a simple example:
The above would execute the given query, substituting myColumn in place of :something before executing the query The doneWithQuery() method indicates that we are not going to use the results for anything. This method discards the results.
A more typical use of the query() function would involve inserting data using appendColumn(), prependColumn(), columnBefore(), or columnAfter(). See the main documentation for QPDB to view a more realistic example.
Method queryInto (line 444)
Run a query and inject the results directly into the elements in the QueryPath object.
If the third argument is empty, the data will be inserted directly into the QueryPath elements unaltered. However, if a template is provided in the third parameter, the query data will be merged into that template and then be added to each QueryPath element.
The template will be merged once for each row, even if no row data is appended into the template.
A template is simply a piece of markup labeled for insertion of data. See QPTPL and QPTPL.php for more information.
Since this does not use a stanard query(), there is no need to call doneWithQuery() after this method.
Method withEachRow (line 541)
This is used primarily to instruct QPDB to iterate through all of the rows when appending, prepending, inserting before, or inserting after.