Class for connecting to SQL databases and performing common operations.

category Zend
package Zend_Db
subpackage Adapter
copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
license New BSD License
inherited_from \Zend_Db_Adapter_Abstract

 Methods

Constructor.

__construct(array|\Zend_Config $config) 
Inherited

$config is an array of key/value pairs or an instance of Zend_Config containing configuration options. These options are common to most adapters:

dbname => (string) The name of the database to user username => (string) Connect to the database as this username. password => (string) Password associated with the username. host => (string) What host to connect to, defaults to localhost

Some options are used on a case-by-case basis by adapters:

port => (string) The port of the database persistent => (boolean) Whether to use a persistent connection or not, defaults to false protocol => (string) The network protocol, defaults to TCPIP caseFolding => (int) style of case-alteration used for identifiers socket => (string) The socket or named pipe that should be used

Parameters

$config

array\Zend_Config

An array or instance of Zend_Config having configuration data

Exceptions

\Zend_Db_Adapter_Exception

called when object is getting serialized This disconnects the DB object that cant be serialized

__sleep() : array
Inherited

Exceptions

\Zend_Db_Adapter_Exception

Returns

array

called when object is getting unserialized

__wakeup() : void
Inherited

Leave autocommit mode and begin a transaction.

beginTransaction() : \Zend_Db_Adapter_Abstract
Inherited

Returns

\Zend_Db_Adapter_Abstract

Force the connection to close.

closeConnection() : void
inherited_from \Zend_Db_Adapter_Abstract::closeConnection()

Force the connection to close.

closeConnection() : void
Inherited

Commit a transaction and return to autocommit mode.

commit() : \Zend_Db_Adapter_Abstract
Inherited

Returns

\Zend_Db_Adapter_Abstract

Deletes table rows based on a WHERE clause.

delete(mixed $table, mixed $where = '') : integer
Inherited

Parameters

$table

mixed

The table to update.

$where

mixed

DELETE WHERE clause(s).

Returns

integerThe number of affected rows.

Returns the column descriptions for a table.

describeTable(string $tableName, string $schemaName = null) : array

The return value is an associative array keyed by the column name, as returned by the RDBMS.

The value of each array element is an associative array with the following keys:

SCHEMA_NAME => string; name of database or schema TABLE_NAME => string; COLUMN_NAME => string; column name COLUMN_POSITION => number; ordinal position of column in table DATA_TYPE => string; SQL datatype name of column DEFAULT => string; default expression of column, null if none NULLABLE => boolean; true if column can have nulls LENGTH => number; length of CHAR/VARCHAR SCALE => number; scale of NUMERIC/DECIMAL PRECISION => number; precision of NUMERIC/DECIMAL UNSIGNED => boolean; unsigned property of an integer type PRIMARY => boolean; true if column is part of the primary key PRIMARY_POSITION => integer; position of column in primary key IDENTITY => integer; true if column is auto-generated with unique values

inherited_from \Zend_Db_Adapter_Abstract::describeTable()

Parameters

$tableName

string

$schemaName

string

OPTIONAL

Returns

array

Returns the column descriptions for a table.

describeTable(string $tableName, string $schemaName = null) : array
Inherited

The return value is an associative array keyed by the column name, as returned by the RDBMS.

The value of each array element is an associative array with the following keys:

SCHEMA_NAME => string; name of database or schema TABLE_NAME => string; COLUMN_NAME => string; column name COLUMN_POSITION => number; ordinal position of column in table DATA_TYPE => string; SQL datatype name of column DEFAULT => string; default expression of column, null if none NULLABLE => boolean; true if column can have nulls LENGTH => number; length of CHAR/VARCHAR SCALE => number; scale of NUMERIC/DECIMAL PRECISION => number; precision of NUMERIC/DECIMAL UNSIGNED => boolean; unsigned property of an integer type PRIMARY => boolean; true if column is part of the primary key PRIMARY_POSITION => integer; position of column in primary key

Parameters

$tableName

string

$schemaName

string

OPTIONAL

Returns

array

Fetches all SQL result rows as a sequential array.

fetchAll(string|\Zend_Db_Select $sql, mixed $bind = array(), mixed $fetchMode = null) : array
Inherited

Uses the current fetchMode for the adapter.

Parameters

$sql

string\Zend_Db_Select

An SQL SELECT statement.

$bind

mixed

Data to bind into SELECT placeholders.

$fetchMode

mixed

Override current fetch mode.

Returns

array

Fetches all SQL result rows as an associative array.

fetchAssoc(string|\Zend_Db_Select $sql, mixed $bind = array()) : array
Inherited

The first column is the key, the entire row array is the value. You should construct the query to be sure that the first column contains unique values, or else rows with duplicate values in the first column will overwrite previous data.

Parameters

$sql

string\Zend_Db_Select

An SQL SELECT statement.

$bind

mixed

Data to bind into SELECT placeholders.

Returns

array

Fetches the first column of all SQL result rows as an array.

fetchCol(string|\Zend_Db_Select $sql, mixed $bind = array()) : array
Inherited

Parameters

$sql

string\Zend_Db_Select

An SQL SELECT statement.

$bind

mixed

Data to bind into SELECT placeholders.

Returns

array

Fetches the first column of the first row of the SQL result.

fetchOne(string|\Zend_Db_Select $sql, mixed $bind = array()) : string
Inherited

Parameters

$sql

string\Zend_Db_Select

An SQL SELECT statement.

$bind

mixed

Data to bind into SELECT placeholders.

Returns

string

Fetches all SQL result rows as an array of key-value pairs.

fetchPairs(string|\Zend_Db_Select $sql, mixed $bind = array()) : array
Inherited

The first column is the key, the second column is the value.

Parameters

$sql

string\Zend_Db_Select

An SQL SELECT statement.

$bind

mixed

Data to bind into SELECT placeholders.

Returns

array

Fetches the first row of the SQL result.

fetchRow(string|\Zend_Db_Select $sql, mixed $bind = array(), mixed $fetchMode = null) : mixed
Inherited

Uses the current fetchMode for the adapter.

Parameters

$sql

string\Zend_Db_Select

An SQL SELECT statement.

$bind

mixed

Data to bind into SELECT placeholders.

$fetchMode

mixed

Override current fetch mode.

Returns

mixedArray, object, or scalar depending on fetch mode.

Helper method to change the case of the strings used when returning result sets in FETCH_ASSOC and FETCH_BOTH modes.

foldCase(string $key) : string
Inherited

This is not intended to be used by application code, but the method must be public so the Statement class can invoke it.

Parameters

$key

string

Returns

string

Returns the configuration variables in this adapter.

getConfig() : array
Inherited

Returns

array

Returns the underlying database connection object or resource.

getConnection() : object | resource | null
Inherited

If not presently connected, this initiates the connection.

Returns

objectresourcenull

Get the fetch mode.

getFetchMode() : integer
Inherited

Returns

integer

Returns the profiler for this adapter.

getProfiler() : \Zend_Db_Profiler
Inherited

Returns

\Zend_Db_Profiler

Returns the symbol the adapter uses for delimiting identifiers.

getQuoteIdentifierSymbol() : string
inherited_from \Zend_Db_Adapter_Abstract::getQuoteIdentifierSymbol()

Returns

string

Returns the symbol the adapter uses for delimited identifiers.

getQuoteIdentifierSymbol() : string
Inherited

Returns

string

Retrieve server version in PHP style

getServerVersion() : string
inherited_from \Zend_Db_Adapter_Abstract::getServerVersion()

Returns

string

Retrieve server version in PHP style

getServerVersion() : string
Inherited

Returns

string

Get the default statement class.

getStatementClass() : string
Inherited

Returns

string

Inserts a table row with specified data.

insert(mixed $table, array $bind) : integer
Inherited

Parameters

$table

mixed

The table to insert data into.

$bind

array

Column-value pairs.

Exceptions

\Zend_Db_Adapter_Exception

Returns

integerThe number of affected rows.

Test if a connection is active

isConnected() : boolean
inherited_from \Zend_Db_Adapter_Abstract::isConnected()

Returns

boolean

Test if a connection is active

isConnected() : boolean
Inherited

Returns

boolean

Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.

lastInsertId(string $tableName = null, string $primaryKey = null) : string

As a convention, on RDBMS brands that support sequences (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence from the arguments and returns the last id generated by that sequence. On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method returns the last value generated for such a column, and the table name argument is disregarded.

MySQL does not support sequences, so $tableName and $primaryKey are ignored.

todo Return value should be int?
inherited_from \Zend_Db_Adapter_Abstract::lastInsertId()

Parameters

$tableName

string

OPTIONAL Name of table.

$primaryKey

string

OPTIONAL Name of primary key column.

Returns

string

Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.

lastInsertId(string $tableName = null, string $primaryKey = null) : string
Inherited

As a convention, on RDBMS brands that support sequences (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence from the arguments and returns the last id generated by that sequence. On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method returns the last value generated for such a column, and the table name argument is disregarded.

Parameters

$tableName

string

OPTIONAL Name of table.

$primaryKey

string

OPTIONAL Name of primary key column.

Returns

string

Return the most recent value from the specified sequence in the database.

lastSequenceId(string $sequenceName) : string
Inherited

This is supported only on RDBMS brands that support sequences (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.

Parameters

$sequenceName

string

Returns

string

Adds an adapter-specific LIMIT clause to the SELECT statement.

limit(string $sql, integer $count, integer $offset = 0) : string
inherited_from \Zend_Db_Adapter_Abstract::limit()

Parameters

$sql

string

$count

integer

$offset

integer

OPTIONAL

Returns

string

Adds an adapter-specific LIMIT clause to the SELECT statement.

limit(mixed $sql, integer $count, integer $offset = 0) : string
Inherited

Parameters

$sql

mixed

$count

integer

$offset

integer

Returns

string

Returns a list of the tables in the database.

listTables() : array
inherited_from \Zend_Db_Adapter_Abstract::listTables()

Returns

array

Returns a list of the tables in the database.

listTables() : array
Inherited

Returns

array

Generate a new value from the specified sequence in the database, and return it.

nextSequenceId(string $sequenceName) : string
Inherited

This is supported only on RDBMS brands that support sequences (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.

Parameters

$sequenceName

string

Returns

string

Prepare a statement and return a PDOStatement-like object.

prepare(string $sql) : \Zend_Db_Statement_Mysqli
inherited_from \Zend_Db_Adapter_Abstract::prepare()

Parameters

$sql

string

SQL query

Returns

\Zend_Db_Statement_Mysqli

Prepare a statement and return a PDOStatement-like object.

prepare(string|\Zend_Db_Select $sql) : \Zend_Db_Statement | \PDOStatement
Inherited

Parameters

$sql

string\Zend_Db_Select

SQL query

Returns

\Zend_Db_Statement\PDOStatement

Prepares and executes an SQL statement with bound data.

query(mixed $sql, mixed $bind = array()) : \Zend_Db_Statement_Interface
Inherited

Parameters

$sql

mixed

The SQL statement with placeholders. May be a string or Zend_Db_Select.

$bind

mixed

An array of data to bind to the placeholders.

Returns

\Zend_Db_Statement_Interface

Safely quotes a value for an SQL statement.

quote(mixed $value, mixed $type = null) : mixed
Inherited

If an array is passed as the value, the array values are quoted and then returned as a comma-separated string.

Parameters

$value

mixed

The value to quote.

$type

mixed

OPTIONAL the SQL datatype name, or constant, or null.

Returns

mixedAn SQL-safe quoted value (or string of separated values).

Quote a column identifier and alias.

quoteColumnAs(string|array|\Zend_Db_Expr $ident, string $alias, boolean $auto = false) : string
Inherited

Parameters

$ident

stringarray\Zend_Db_Expr

The identifier or expression.

$alias

string

An alias for the column.

$auto

boolean

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

Returns

stringThe quoted identifier and alias.

Quotes an identifier.

quoteIdentifier(string|array|\Zend_Db_Expr $ident, boolean $auto = false) : string
Inherited

Accepts a string representing a qualified indentifier. For Example: $adapter->quoteIdentifier('myschema.mytable') Returns: "myschema"."mytable"

Or, an array of one or more identifiers that may form a qualified identifier: $adapter->quoteIdentifier(array('myschema','my.table')) Returns: "myschema"."my.table"

The actual quote character surrounding the identifiers may vary depending on the adapter.

Parameters

$ident

stringarray\Zend_Db_Expr

The identifier.

$auto

boolean

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

Returns

stringThe quoted identifier.

Quotes a value and places into a piece of text at a placeholder.

quoteInto(string $text, mixed $value, string $type = null, integer $count = null) : string
Inherited

The placeholder is a question-mark; all placeholders will be replaced with the quoted value. For example:

$text = "WHERE date < ?"; $date = "2005-01-02"; $safe = $sql->quoteInto($text, $date); // $safe = "WHERE date < '2005-01-02'"

Parameters

$text

string

The text with a placeholder.

$value

mixed

The value to quote.

$type

string

OPTIONAL SQL datatype

$count

integer

OPTIONAL count of placeholders to replace

Returns

stringAn SQL-safe quoted value placed into the original text.

Quote a table identifier and alias.

quoteTableAs(string|array|\Zend_Db_Expr $ident, string $alias = null, boolean $auto = false) : string
Inherited

Parameters

$ident

stringarray\Zend_Db_Expr

The identifier or expression.

$alias

string

An alias for the table.

$auto

boolean

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

Returns

stringThe quoted identifier and alias.

Roll back a transaction and return to autocommit mode.

rollBack() : \Zend_Db_Adapter_Abstract
Inherited

Returns

\Zend_Db_Adapter_Abstract

Creates and returns a new Zend_Db_Select object for this adapter.

select() : \Zend_Db_Select
Inherited

Returns

\Zend_Db_Select

Set the fetch mode.

setFetchMode(integer $mode) : void
inherited_from \Zend_Db_Adapter_Abstract::setFetchMode()

Parameters

$mode

integer

Exceptions

\Zend_Db_Adapter_Mysqli_Exception

Set the fetch mode.

setFetchMode(integer $mode) : void
Inherited

Parameters

$mode

integer

Exceptions

\Zend_Db_Adapter_Exception

Set the adapter's profiler object.

setProfiler(\Zend_Db_Profiler|\Zend_Config|array|boolean $profiler) : \Zend_Db_Adapter_Abstract
Inherited

The argument may be a boolean, an associative array, an instance of Zend_Db_Profiler, or an instance of Zend_Config.

A boolean argument sets the profiler to enabled if true, or disabled if false. The profiler class is the adapter's default profiler class, Zend_Db_Profiler.

An instance of Zend_Db_Profiler sets the adapter's instance to that object. The profiler is enabled and disabled separately.

An associative array argument may contain any of the keys 'enabled', 'class', and 'instance'. The 'enabled' and 'instance' keys correspond to the boolean and object types documented above. The 'class' key is used to name a class to use for a custom profiler. The class must be Zend_Db_Profiler or a subclass. The class is instantiated with no constructor arguments. The 'class' option is ignored when the 'instance' option is supplied.

An object of type Zend_Config may contain the properties 'enabled', 'class', and 'instance', just as if an associative array had been passed instead.

Parameters

$profiler

\Zend_Db_Profiler\Zend_Configarrayboolean

Exceptions

\Zend_Db_Profiler_Exception if the object instance or class specified is not Zend_Db_Profiler or an extension of that class.

Returns

\Zend_Db_Adapter_AbstractProvides a fluent interface

Set the default statement class.

setStatementClass($class) : \Zend_Db_Adapter_Abstract
Inherited

Parameters

$class

Returns

\Zend_Db_Adapter_AbstractFluent interface

Check if the adapter supports real SQL parameters.

supportsParameters(string $type) : boolean
inherited_from \Zend_Db_Adapter_Abstract::supportsParameters()

Parameters

$type

string

'positional' or 'named'

Returns

boolean

Check if the adapter supports real SQL parameters.

supportsParameters(string $type) : boolean
Inherited

Parameters

$type

string

'positional' or 'named'

Returns

boolean

Updates table rows with specified data based on a WHERE clause.

update(mixed $table, array $bind, mixed $where = '') : integer
Inherited

Parameters

$table

mixed

The table to update.

$bind

array

Column-value pairs.

$where

mixed

UPDATE WHERE clause(s).

Exceptions

\Zend_Db_Adapter_Exception

Returns

integerThe number of affected rows.

Begin a transaction.

_beginTransaction() : void
inherited_from \Zend_Db_Adapter_Abstract::_beginTransaction()

Begin a transaction.

_beginTransaction() 
Inherited

Check for config options that are mandatory.

_checkRequiredOptions(array $config) 
Inherited

Throw exceptions if any are missing.

Parameters

$config

array

Exceptions

\Zend_Db_Adapter_Exception

Commit a transaction.

_commit() : void
inherited_from \Zend_Db_Adapter_Abstract::_commit()

Commit a transaction.

_commit() 
Inherited

Creates a connection to the database.

_connect() : void
inherited_from \Zend_Db_Adapter_Abstract::_connect()

Exceptions

\Zend_Db_Adapter_Mysqli_Exception

Creates a connection to the database.

_connect() : void
Inherited

Quote a raw string.

_quote(mixed $value) : string
inherited_from \Zend_Db_Adapter_Abstract::_quote()

Parameters

$value

mixed

Raw string

Returns

stringQuoted string

Quote a raw string.

_quote(string $value) : string
Inherited

Parameters

$value

string

Raw string

Returns

stringQuoted string

Quote an identifier.

_quoteIdentifier(string $value, boolean $auto = false) : string
Inherited

Parameters

$value

string

The identifier or expression.

$auto

boolean

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

Returns

stringThe quoted identifier and alias.

Quote an identifier and an optional alias.

_quoteIdentifierAs(string|array|\Zend_Db_Expr $ident, string $alias = null, boolean $auto = false, string $as = ' AS ') : string
Inherited

Parameters

$ident

stringarray\Zend_Db_Expr

The identifier or expression.

$alias

string

An optional alias.

$auto

boolean

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

$as

string

The string to add between the identifier/expression and the alias.

Returns

stringThe quoted identifier and alias.

Roll-back a transaction.

_rollBack() : void
inherited_from \Zend_Db_Adapter_Abstract::_rollBack()

Roll-back a transaction.

_rollBack() 
Inherited

Convert an array, string, or Zend_Db_Expr object into a string to put in a WHERE clause.

_whereExpr(mixed $where) : string
Inherited

Parameters

$where

mixed

Returns

string

 Properties

 

Weither or not that object can get serialized

$_allowSerialization : boolean

Default

true
 

Specifies whether the adapter automatically quotes identifiers.

$_autoQuoteIdentifiers : boolean

Default

true

If true, most SQL generated by Zend_Db classes applies identifier quoting automatically. If false, developer must quote identifiers themselves by calling quoteIdentifier().

 

Weither or not the database should be reconnected to that adapter when waking up

$_autoReconnectOnUnserialize : boolean

Default

false
 

Specifies the case of column names retrieved in queries Options Zend_Db::CASE_NATURAL (default) Zend_Db::CASE_LOWER Zend_Db::CASE_UPPER

$_caseFolding : integer

Default

\Zend_Db::CASE_NATURAL
 

User-provided configuration

$_config : array

Default

array()
 

Database connection

$_connection : object | resource | null

Default

null
 

Default class name for the profiler object.

$_defaultProfilerClass : string

Default

'Zend_Db_Profiler'
 

Default class name for a DB statement.

$_defaultStmtClass : string

Default

'Zend_Db_Statement_Mysqli'
inherited_from \Zend_Db_Adapter_Abstract::_defaultStmtClass
 

Default class name for a DB statement.

$_defaultStmtClass : string

Default

'Zend_Db_Statement'
 

Fetch mode

$_fetchMode : integer

Default

\Zend_Db::FETCH_ASSOC
 

Keys are UPPERCASE SQL datatypes or the constants Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.

$_numericDataTypes : array

Default

array(\Zend_Db::INT_TYPE => \Zend_Db::INT_TYPE, \Zend_Db::BIGINT_TYPE => \Zend_Db::BIGINT_TYPE, \Zend_Db::FLOAT_TYPE => \Zend_Db::FLOAT_TYPE, 'INT' => \Zend_Db::INT_TYPE, 'INTEGER' => \Zend_Db::INT_TYPE, 'MEDIUMINT' => \Zend_Db::INT_TYPE, 'SMALLINT' => \Zend_Db::INT_TYPE, 'TINYINT' => \Zend_Db::INT_TYPE, 'BIGINT' => \Zend_Db::BIGINT_TYPE, 'SERIAL' => \Zend_Db::BIGINT_TYPE, 'DEC' => \Zend_Db::FLOAT_TYPE, 'DECIMAL' => \Zend_Db::FLOAT_TYPE, 'DOUBLE' => \Zend_Db::FLOAT_TYPE, 'DOUBLE PRECISION' => \Zend_Db::FLOAT_TYPE, 'FIXED' => \Zend_Db::FLOAT_TYPE, 'FLOAT' => \Zend_Db::FLOAT_TYPE)

Values are: 0 = 32-bit integer 1 = 64-bit integer 2 = float or decimal

inherited_from \Zend_Db_Adapter_Abstract::_numericDataTypes
 

Keys are UPPERCASE SQL datatypes or the constants Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.

$_numericDataTypes : array

Default

array(\Zend_Db::INT_TYPE => \Zend_Db::INT_TYPE, \Zend_Db::BIGINT_TYPE => \Zend_Db::BIGINT_TYPE, \Zend_Db::FLOAT_TYPE => \Zend_Db::FLOAT_TYPE)

Values are: 0 = 32-bit integer 1 = 64-bit integer 2 = float or decimal

 

Query profiler object, of type Zend_Db_Profiler or a subclass of that.

$_profiler : \Zend_Db_Profiler

Default

 

$_stmt

$_stmt : \Zend_Db_Statement_Mysqli

Default

null