Connect using custom link

$database = new \db\database (string $database, \db\link $link);

But befure discussing it let me introduce \db\link to you.

By default db.php establishes connections using \db\link class which by itself is wrapper of php built in PDO class.

$link = new \db\link (string $name, string $hostname, string $username, string $password, array $config);

name It is unique name of the link and you will use it later. For example it might be named like 'my_mysql_link'.

hostname It is actually a data source name. db.php uses PDO as default link provider so first parameter is PDO data source name string.

username This is what you think.

password Same applies here.

config By default \db\link is PDO wrapper and here you can pass PDO configuration options. Default value of config is


You can also first init $link = new \db\link(...) and than set custom configuration option like:

$link->config[\PDO::MYSQL_ATTR_READ_DEFAULT_FILE] = '/etc/my.cnf'

Link goes as a second parameter of \db\database constructor. If you are passing link than first parameter is default database name for your tables.

$database = new \db\database (string $database, \db\link $link);

database Default database name for your tables


$database = new \db\database ('my_db', new \db\link ('my_mysql_link', 'mysql:host=', 'my_user', 'my_pass'));

After creating you can access your link by name:


First link is considered as default link. Default link can be accessed without parameter:


Overriding default link class

You can override link and develop your own. Just look at code of class \db\link and make same methods. It is done without interface. db.php even does not check where do your link objects come it just requires that that they had following methods:


Executes query and returns result array or resource iteratable as array if any. Returns null if resultless query or false if query has no results.

[array] public function query (string $query)

Returns result array or resource iteratable as array or false if no records. Array must contain values with numerical keys begining with 0 in natural select field order. Like if we select field1,field2 from table, result record array must contain $row[0] = 'field 1 value', $row[1] = 'field 2 value'

array public function select (string $query)

Fetches first record of result. Result must contain values with numerical keys begining with 0 in natural select field order.

array public function fetch (string $query)

Fetches first value of first record of result.

string public function value (string $query)

Must return true or false if passed $code patarmeter and $code parameter equals actual error code that had place. Or must return error information if no $code parameter passed. Comparing passed $code and returning true or false is important part of link class while autmaticaly generating database structure.

boolean/mixed public function error (integer $code=null)

Returns last inserted id for that connection

integer public function id ()

For further reading see sample codes using only \db\link https://github.com/hazardland/db.php/blob/master/samples/002.link.php