DB class

Database connection class.

Class Overview

Database connection class.

This class is used to represent a database connection. It support multiple backends.

This class provides a get_instance convenience method that eases development with just one database connection (as is the case with almost all applications).

Public Static Methods

static get_instance($type=null, $settings=null) [static]

Obtain a database instance.

This method is modeled after the singleton design pattern, but you will need to initialize it with the connection settings. The first call to this method will initialize the database with the provided settings. Subsequent files return a reference to the database instance (and paramaters are ignored).

Parameters

$type

The type of database to be used.

$settings

An associative array of connection settings.

See also

Public Methods

DB($type='mysql', $settings=null)

Constructor initalizes the database layer.

Parameters

$type

The type of database to be used (e.g. mysql or sqlite).

$settings

Optional argument to be passed to connect().

See also

setup($settings)

Prepares the database layer for deferred connection.

Parameters

$settings

Associative array of connection settings.

connect($settings=null)

Connects to the database using the specified connection settings.

Parameters

$settings

An associative array of connection settings. Set the debug and debug_print keys to true if you want to debug your queries.

disconnect()

Disconnects from the database.

Not calling this method is harmless for most database backends.

select_db($name)

Selects the database to be used.

Parameters

$name

The database name.

Query methods

prepare($sql)

Prepares a query for execution.

Parameters

$sql

The SQL query to be prepared. Use ?fieldtype? strings.

Return value

A new PreparedQuery instance that can be executed.

prepare_execute_fetch_all($sql, $params=null)

Convenience method to quickly execute a single query and fetch all resulting rows from the result set.

If you want to use a query multiple times, you should obtain a PreparedQuery object by calling the prepare() method, execute it and operate on the ResultSet object.

Parameters

$sql

The SQL query to execute. Placeholders (e.g. ?int?) should be used.

$params

Zero or more parameters to be filled in in the $query. The number of parameters should match the number of placeholders in $query

See also

prepare_execute_fetch($sql, $params=null)

Convenience method to quickly execute a single query and fetch one resulting row from the result set.

See DB::prepare_execute_fetch_all for more information.

Parameters

$sql

The SQL query to execute.

$params

Zero or more parameters to be filled in in the $query.

See also

prepare_execute($sql, $params=null)

Convenience method to quickly execute a single query.

No results are retrieved. This method is pretty useless for SELECT queries, the number of affected rows is returned instead of result rows. See DB::prepare_execute_fetch_all for more information.

Parameters

$sql

The SQL query to execute.

$params

Zero or more parameters to be filled in in the $query.

Return value

The number of rows affected by the query. This only works for queries that operate on a number of rows, i.e. INSERT, UPDATE, REPLACE, and DELETE queries. For other query types null is returned.

See also

Transaction methods

transaction_begin()

Starts a transaction.

This method calls into the backend.

transaction_commit()

Commits a transaction.

This method calls into the backend.

transaction_rollback()

Rolls back a transaction.

This method calls into the backend.

Private Attributes

$type [private]

Type of database being used.

$backend [private]

Database backend instance.

$databasename [private]

Current database name.

$connected [private]

Boolean indication the connection status.

$last_query [private]

Last query executed.

$num_queries_executed [private]

Counter for the number of queries executed.

$settings [private]

Settings for DB::setup($settings).

$debug [private]

Enable/disable debugging.

$debug_print [private]

Print queries before execution.

$queries_executed [private]

List of executed queries (only used if debugging is on).