Simple function for returning paged result sets.
<?php
// use: pagedQuery($link, $sql, $rows_per_page, $current_page)
function pagedQuery($link, $sql, $nrows = 10, $page = 1) {
$handle = dbx_query($link, $sql, DBX_RESULT_UNBUFFERED);
$result = new stdClass;
$result->link = $this->link;
$result->page = $page;
$result->data = array();
$result->info['name'] = array();
$start = ($page - 1) * $nrows;
$end = $start + $nrows;
$result->rows = 0;
while ($row = dbx_fetch_row($handle)) {
$result->rows++;
if (($result->rows > $start) && ($result->rows <= $end)) {
$result->data[] = $row;
}
}
if ($result->data[0]) {
$result->cols = count($result->data[0]) / 2;
$result->info['name'] = array_slice(array_keys($result->data[0]), $result->cols, $result->cols);
$result->lastPage = ceil($result->rows / $nrows);
}
return $result;
}
?>
XXVII. dbx Functions
Úvod
The dbx module is a database abstraction layer (db 'X', where 'X' is a supported database). The dbx functions allow you to access all supported databases using a single calling convention. The dbx-functions themselves do not interface directly to the databases, but interface to the modules that are used to support these databases.
Poznámka: Toto rozšíření bylo přesunuto do repozitáře » PECL a již nebude součástí balíku PHP.5.1.0.
Požadavky
To be able to use a database with the dbx-module, the module must be either linked or loaded into PHP, and the database module must be supported by the dbx-module. Currently, the following databases are supported, but others will follow:
- FrontBase (available from PHP 4.1.0).
- Microsoft SQL Server
- MySQL
- ODBC
- PostgreSQL
- Sybase-CT (available from PHP 4.2.0).
- Oracle (oci8) (available from PHP 4.3.0).
- SQLite (PHP 5).
Documentation for adding additional database support to dbx can be found at » http://www.guidance.nl/php/dbx/doc/.
Instalace
In order to have these functions available, you must compile PHP with dbx support by using the --enable-dbx option and all options for the databases that will be used, e.g. for MySQL you must also specify --with-mysql=[DIR]. To get other supported databases to work with the dbx-module refer to their specific documentation.
Konfigurace běhu
Chování těchto funkcí je ovlivněno nastavením parametrů v php.ini.
Tabulka 43. DBX Configuration Options
| Name | Default | Changeable | Changelog |
|---|---|---|---|
| dbx.colnames_case | "unchanged" | PHP_INI_SYSTEM | Available since PHP 4.3.0. |
Pro další detaily a definice konstant PHP_INI_*, viz dokumentace k ini_set().
Zde je stručný popis konfiguračních direktiv.
- dbx.colnames_case string
Columns names can be returned "unchanged" or converted to "uppercase" or "lowercase". This directive can be overridden with a flag to dbx_query().
Typy prostředků
There are two resource types used in the dbx module. The first one is the link-object for a database connection, the second a result-object which holds the result of a query.
Předdefinované konstanty
Tyto konstanty jsou definovány tímto rozšířením a budou k dispozici pouze tehdy, bylo-li rozšíření zkompilováno společně s PHP nebo dynamicky zavedeno za běhu.
- DBX_MYSQL (integer)
- DBX_ODBC (integer)
- DBX_PGSQL (integer)
- DBX_MSSQL (integer)
- DBX_FBSQL (integer)
- DBX_OCI8 (integer) (available from PHP 4.3.0)
- DBX_SYBASECT (integer)
- DBX_SQLITE (integer) (PHP 5)
- DBX_PERSISTENT (integer)
- DBX_RESULT_INFO (integer)
- DBX_RESULT_INDEX (integer)
- DBX_RESULT_ASSOC (integer)
- DBX_RESULT_UNBUFFERED (integer) (PHP 5)
- DBX_COLNAMES_UNCHANGED (integer) (available from PHP 4.3.0)
- DBX_COLNAMES_UPPERCASE (integer) (available from PHP 4.3.0)
- DBX_COLNAMES_LOWERCASE (integer) (available from PHP 4.3.0)
- DBX_CMP_NATIVE (integer)
- DBX_CMP_TEXT (integer)
- DBX_CMP_NUMBER (integer)
- DBX_CMP_ASC (integer)
- DBX_CMP_DESC (integer)
Obsah
- dbx_close — Close an open connection/database
- dbx_compare — Compare two rows for sorting purposes
- dbx_connect — Open a connection/database
- dbx_error — Report the error message of the latest function call in the module
- dbx_escape_string — Escape a string so it can safely be used in an sql-statement
- dbx_fetch_row — Fetches rows from a query-result that had the DBX_RESULT_UNBUFFERED flag set
- dbx_query — Send a query and fetch all results (if any)
- dbx_sort — Sort a result from a dbx_query by a custom sort function
dbx Functions
08-Jul-2004 06:45
01-Jul-2004 08:12
Would like to confirm that dbx is at least three times faster than adodb with my application. However, once loaded, adodb has much more features and probably performs better in some situations then dbx.
One of the nice features that adodb has, is the way you can quickly make a dropdown menu from a query. Here's a similar function that works with a dbx result object:
<?php
// $result is the dbx_query result (It expects two fields)
// $name is the name of the dropdown field
// $selected is the option that should be selected
// $firstrow takes a string like "myvalue:myname"
// $attr can be used to add some extra attributes
function dropDown($result, $name, $selected, $firstRow, $attr) {
$s = '<select name="'.$name.'" '.$attr.'>';
if (is_string($firstRow)) {
$row = explode(':', $firstRow);
$s .= '<option value="'.$row[0].'">'.$row[1].'</option>';
} else
$s .= '<option></option>';
foreach ($result->data as $row) {
if ($row[0] == $selected)
$s .= '<option value="'.$row[0].'" selected="selected">'.htmlspecialchars($row[1]).'</option>';
else
$s .= '<option value="'.$row[0].'">'.htmlspecialchars($row[1]).'</option>';
}
return $s.'</select>';
}
?>
25-Nov-2003 01:52
BEWARE!!!
dbx_query allocate all retrieved data in an array on memory. If the query result is big (bigger then the memory_limit on php.ini), the result will be empty. So use dbx wih care.
07-Jun-2003 01:49
The above benchmark is very misleading. Unfortunately, I found out the hard way by porting my app to ADODB. Most PHP apps are going to be very short-lived therefore startup / setup time is a large factor. This benchmark does not measure that. Just include()ing the main ADODB file added 27ms to my app's startup time! Considering my app took only 16ms to run in total using DBX, the idea of switching to ADODB was DOA before running a single query.
In the end, the total time for my app went from 16ms with DBX to 49ms with ADODB. ADODB's powerful features are obviously not worth that kind of overhead.
Carl Thompson
06-Feb-2003 07:35
For some benchmarks of dbx connecting to mysql compared to native mysql api, adodb, and others, see:
http://phplens.com/lens/adodb/
http://php.weblogs.com/2003/02/06#a2336
