Why another database layer?

PHP provides numerous database access layers already; however, waf provides a (mostly) very thin abstraction layer on top of the mysql access module. This layer includes query preparation and execution functions that don't exist in the native layer, and automatic database initialization and cleanup. Use of the waf database functions is not required, you can still use any other database access method you wish.

Clean up the database connection. This is implicitly called after any page that initializes the database.
Returns a string describing the error that occured in the last query, or NULL if no error occured.
Escapes a string for use in an SQL query, alias for mysql_real_escape_string()
Initialize the database connection, if it hasn't been already.
If the last query was an INSERT statement into a table with an AUTO_INCREMENT column, returns the last number that was generated for that column.
dbPrepareQuery($Query [, ...])
Works along the lines of the C sprintf() function, used for parameterizing queries. Using the first parameter as a template, data placeholders are replaced by properly escaped data by the additional parameters. There should be one parameter for each placeholder; if there are too few parameters then the placeholders are not replaced; if there are too many parameters than the ones for which there are no placeholders are silently ignored. The properly escaped string is returned.
The database connection must be initialized for this function to work, as it relies on the mysql_real_escape_string() function.
Placeholders are as follows:
  • %d - Integer
  • %f - Floating-point number
  • %s - String
  • %t - Unquoted string (table name)
  • %% - Literal percent-sign (%), does not occupy a parameter
Executes a SQL query, discarding all results.
Executes a SQL query, returning all results in an array of associative arrays. Be careful when using this with queries having large result sets, it will consume a great deal of memory.
Executes a SQL query, returning a scalar value from the first column of the first row of results.
Executes a SQL query, returning an associative array of the first row of results.

Example 1: Basic Query

<?php $Page['title'] = 'Results'; dbInit(); $Threshold = isset($_REQUEST['threshold']) ? $_REQUEST['threshold'] : 0; $Count = dbQueryOneCell( dbPrepareQuery( "SELECT COUNT(*) FROM t_widgets WHERE quantity <= %d", $Threshold) ); $Page['body'] = '<p>There are currently ' . $Count . ' widgets with a quantity under the specified threshold.</p>'; ?>

This is a very basic query illustrating the basic use of the database functions and formatting some output.

Example 2: Bigger Query

<?php $Page['title'] = 'Results'; $ReportsTable = 't_reports'; $WidgetsTable = 't_widgets'; dbInit(); $Threshold = isset($_REQUEST['threshold']) ? $_REQUEST['threshold'] : 0; dbQuery(dbPrepareQuery("UPDATE %t SET last_executed = NOW() WHERE report_name = %s", $ReportsTable, $Page['name']) ); $Widgets = dbQueryOneCell( dbPrepareQuery( "SELECT quantity,stock_number,description FROM %t WHERE quantity <= %d", $WidgetsTable, $Threshold) ); $Page['body'] = "<p>The following widgets are currently under the specified threshold of ' . $Threshold . '.</p> <table> <thead> <tr> <td>Stock #</td> <td>Qty</td> <td>Description</td> </tr> </thead>\n"; foreach($Widgets as $Widget) { $Page['body'] .= ' <tr> <td>' . $Widget['stock_number'] . '</td> <td>' . $Widget['quantity'] . '</td> <td>' . $Widget['description'] . '</td> </tr>'; } $Page['body'] .= ' </table>'; ?>
This page last updated: Mon Feb 25 22:04:22 2013 (GMT)