dbx_connect

(PHP 4 >= 4.0.6, PHP 5 <= 5.0.4)

dbx_connect -- Open a connection/database

Description

object dbx_connect ( mixed module, string host, string database, string username, string password [, int persistent] )

dbx_connect() returns an object on success, FALSE on error. If a connection has been made but the database could not be selected, the connection is closed and FALSE is returned. The persistent parameter can be set to DBX_PERSISTENT, if so, a persistent connection will be created.

The module parameter can be either a string or a constant, though the latter form is preferred. The possible values are given below, but keep in mind that they only work if the module is actually loaded.

  • DBX_MYSQL or "mysql"

  • DBX_ODBC or "odbc"

  • DBX_PGSQL or "pgsql"

  • DBX_MSSQL or "mssql"

  • DBX_FBSQL or "fbsql" (available from PHP 4.1.0)

  • DBX_SYBASECT or "sybase_ct" (available from PHP 4.2.0)

  • DBX_OCI8 or "oci8" (available from PHP 4.3.0)

  • DBX_SQLITE or "sqlite" (PHP 5)

The host, database, username and password parameters are expected, but not always used depending on the connect functions for the abstracted module.

The returned object has three properties:

database

It is the name of the currently selected database.

handle

It is a valid handle for the connected database, and as such it can be used in module-specific functions (if required).

<?php
$link
= dbx_connect(DBX_MYSQL, "localhost", "db", "username", "password");
mysql_close($link->handle); // dbx_close($link) would be better here
?>

module

It is used internally by dbx only, and is actually the module number mentioned above.

例子 1. dbx_connect() example

<?php
$link
= dbx_connect(DBX_ODBC, "", "db", "username", "password", DBX_PERSISTENT)
    or die(
"Could not connect");

echo
"Connected successfully";
dbx_close($link);
?>

注: Always refer to the module-specific documentation as well.

See also dbx_close().


add a note add a note User Contributed Notes
Jon Moss
04-Mar-2005 10:50
I have been using dbx for a product at work and the need to use SQLite as a database popped up. However, I couldn't find any references to using it with dbx other than that you could and since SQLite doesn't seem to do the username password thing I was a little confused as to how to use it in dbx.

With a little research it seems that when using dbx_connect you only need to fill in the module name ('DBX_SQLITE') and the database (the file name for the database!) to get it working. e.g.

$link = dbx_connect(DBX_SQLITE, "", "some.db", "", "");

thought someone might find it useful, especially since it no longer required additional dll's in version 5.x
editor at plummi dot de
26-Jan-2005 09:44
Pear:DB ist a great Idea, but it`s heavy and slower. I`ve tested dbx and Pear:DB with MySQL 4.0.23_Debian-1-log.

With Pear the Script used around 500 KiB of Memory and needed about 0.15s to 0.25s to complete.
With dbx it used around 80 KiB of Memory and needed around 0.09s to 0.11s to complete!

For ME, dbx is absolutly the best abstraktion around! :-)
Nice Work!!

Sascha Plumhoff
BDKR at melnabone at mindless dt com
08-Dec-2002 02:18
The advantage that this has over Pear, Adob, and even Eclipse is speed. As some one put elsewhere,

"Yes, there is an advantage: Speed!
ext/dbx is written in C and has to be compiled into PHP. It's just faster then the PEAR classes."

Cheers,
BDKR
webmaster at wizactive dot com
22-Feb-2002 10:39
ADOdb seems to be the best db-abstraction library in PHP. It's very fast also. You can get it from ADOdb homepage @ http://php.weblogs.com/adodb/
mboeren at php dot net
19-Feb-2002 08:35
The pgsql connecting as nobody if you don't supply a password issue has been solved in CVS.
davidbullock at tech-center dot com
08-Jan-2002 02:01
Currently to connect to the PostgreSQL backend, you have to supply both a username, and a password or the dbx pgsql module attempts to connect as "nobody".
jeremy at deadbeef dot com
24-Jul-2001 04:54
Pear::DB seems like a better idea than this.  See http://pear.php.net