Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : dbx Functions

dbx Functions

Introduction

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.

Note:

This extension has been moved to the » PECL repository and is no longer bundled with PHP as of PHP 5.1.0.

Requirements

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:

Documentation for adding additional database support to dbx can be found at » http://www.guidance.nl/php/dbx/doc/.

Installation

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.

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 51. DBX Configuration Options

Name Default Changeable Changelog
dbx.colnames_case "unchanged" PHP_INI_SYSTEM Available since PHP 4.3.0. Removed in PHP 5.1.0.


For further details and definitions of the PHP_INI_* constants, see the Appendix I, php.ini directives.

Here's a short explanation of the configuration directives.

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().

Resource Types

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.

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

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)

Table of Contents

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

Code Examples / Notes » ref.dbx

bart

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>';
}
?>


carl

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


bart

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;
}
?>


jlim

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


rhcf

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.


Change Language


Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
CURL
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Net_Gopher
Haru PDF Functions
hash Functions
HTTP
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
IBM DB2
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
IMAP
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
KADM5
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
oggvorbis
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Radius
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
Semaphore
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker