interface DbalInterface (View source)

Main interface for database interactions

Constants

FETCH_NUM

FETCH_ASSOC

Methods

query(string $query, int $connectionType = Connection::TYPE_USER, int $options = 0, bool $cacheAffectedRows = true)

runs a query

mixed
tryQuery(string $query, int $connectionType = Connection::TYPE_USER, int $options = 0, bool $cacheAffectedRows = true)

runs a query and returns the result

bool
tryMultiQuery(string $multiQuery = '', int $connectionType = Connection::TYPE_USER)

Send multiple SQL queries to the database server and execute the first one

array
getTables(string $database, int $connectionType = Connection::TYPE_USER)

returns array with table names for given db

array
getTablesFull(string $database, string|array $table = '', bool $tableIsGroup = false, int $limitOffset = 0, bool|int $limitCount = false, string $sortBy = 'Name', string $sortOrder = 'ASC', string|null $tableType = null, int $connectionType = Connection::TYPE_USER)

returns array of all tables in given db or dbs this function expects unquoted names: RIGHT: my_database WRONG: my_database WRONG: my_database if $tbl_is_group is true, $table is used as filter for table names

array
getVirtualTables(string $db)

Get VIEWs in a particular database

array
getDatabasesFull(string|null $database = null, bool $forceStats = false, int $connectionType = Connection::TYPE_USER, string $sortBy = 'SCHEMA_NAME', string $sortOrder = 'ASC', int $limitOffset = 0, bool|int $limitCount = false)

returns array with databases containing extended infos about them

array
getColumnMapFromSql(string $sqlQuery, array $viewColumns = [])

returns detailed array with all columns for sql

array
getColumnsFull(string|null $database = null, string|null $table = null, string|null $column = null, int $connectionType = Connection::TYPE_USER)

returns detailed array with all columns for given table in database, or all tables/databases

array
getColumn(string $database, string $table, string $column, bool $full = false, int $connectionType = Connection::TYPE_USER)

Returns description of a $column in given table

array
getColumns(string $database, string $table, bool $full = false, int $connectionType = Connection::TYPE_USER)

Returns descriptions of columns in given table

array
getColumnNames(string $database, string $table, int $connectionType = Connection::TYPE_USER)

Returns all column names in given table

array
getTableIndexes(string $database, string $table, int $connectionType = Connection::TYPE_USER)

Returns indexes of a table

false|string|null
getVariable(string $var, int $type = DatabaseInterface::GETVAR_SESSION, int $connectionType = Connection::TYPE_USER)

returns value of given mysql server variable

bool
setVariable(string $var, string $value, int $connectionType = Connection::TYPE_USER)

Sets new value for a variable if it is different from the current value

void
postConnect()

Function called just after a connection to the MySQL database server has been established. It sets the connection collation, and determines the version of MySQL which is running.

void
setCollation(string $collation)

Sets collation connection for user link

void
postConnectControl(Relation $relation)

Function called just after a connection to the MySQL database server has been established. It sets the connection collation, and determines the version of MySQL which is running.

string|false|null
fetchValue(string $query, int|string $field = 0, int $connectionType = Connection::TYPE_USER)

returns a single value from the given result or query, if the query or the result has more than one row or field the first field of the first row is returned

array|null
fetchSingleRow(string $query, string $type = DbalInterface::FETCH_ASSOC, int $connectionType = Connection::TYPE_USER)

Returns only the first row from the result or null if result is empty.

array
fetchResult(string $query, string|int|array $key = null, string|int $value = null, int $connectionType = Connection::TYPE_USER)

returns all rows in the resultset in one array

array
getCompatibilities()

Get supported SQL compatibility modes

array
getWarnings(int $connectionType = Connection::TYPE_USER)

returns warnings for last query

string
getCurrentUser()

gets the current user with host

bool
isSuperUser()

Checks if current user is superuser

bool
isGrantUser()

No description

bool
isCreateUser()

No description

bool
isConnected()

No description

array
getCurrentUserAndHost()

Get the current user and host

int
getLowerCaseNames()

Returns value for lower_case_table_names variable

Connection|null
connect(int $mode, array|null $server = null, int|null $target = null)

Connects to the database server.

bool
selectDb(string|DatabaseName $dbname, int $connectionType = Connection::TYPE_USER)

selects given database

bool
moreResults(int $connectionType = Connection::TYPE_USER)

Check if there are any more query results from a multi query

bool
nextResult(int $connectionType = Connection::TYPE_USER)

Prepare next result from multi_query

mixed
storeResult(int $connectionType = Connection::TYPE_USER)

Store the result returned from multi query

string|bool
getHostInfo(int $connectionType = Connection::TYPE_USER)

Returns a string representing the type of connection used

int|bool
getProtoInfo(int $connectionType = Connection::TYPE_USER)

Returns the version of the MySQL protocol used

string
getClientInfo()

returns a string that represents the client library version

string
getError(int $connectionType = Connection::TYPE_USER)

Returns last error message or an empty string if no errors occurred.

string|int
queryAndGetNumRows(string $query)

returns the number of rows returned by last query used with tryQuery as it accepts false

int
insertId(int $connectionType = Connection::TYPE_USER)

returns last inserted auto_increment id for given $link or $GLOBALS['userlink']

int|string
affectedRows(int $connectionType = Connection::TYPE_USER, bool $getFromCache = true)

returns the number of rows affected by last query

array
getFieldsMeta(ResultInterface $result)

returns metainfo for fields in $result

string
quoteString(string $str, int $connectionType = Connection::TYPE_USER)

Returns properly quoted string for use in MySQL queries.

string
escapeString(string $str, int $connectionType = Connection::TYPE_USER) deprecated

returns properly escaped string for use in MySQL queries

string
escapeMysqlWildcards(string $str)

Returns properly escaped string for use in MySQL LIKE clauses.

bool
isAmazonRds()

Checks if this database server is running on Amazon RDS.

string
getKillQuery(int $process)

Gets SQL for killing a process.

getSystemDatabase()

Get the phpmyadmin database manager

getTable(string $dbName, string $tableName)

Get a table with database name and table name

string
getDbCollation(string $db)

returns collation of given db

string
getServerCollation()

returns default server collation from show variables

int
getVersion()

Server version as number

string
getVersionString()

Server version

string
getVersionComment()

Server version comment

bool
isMariaDB()

Whether connection is MariaDB

bool
isPercona()

Whether connection is Percona

Statement|null
prepare(string $query, int $connectionType = Connection::TYPE_USER)

Prepare an SQL statement for execution.

Details

ResultInterface query(string $query, int $connectionType = Connection::TYPE_USER, int $options = 0, bool $cacheAffectedRows = true)

runs a query

Parameters

string $query

SQL query to execute

int $connectionType
int $options

optional query options

bool $cacheAffectedRows

whether to cache affected rows

Return Value

ResultInterface

mixed tryQuery(string $query, int $connectionType = Connection::TYPE_USER, int $options = 0, bool $cacheAffectedRows = true)

runs a query and returns the result

Parameters

string $query

query to run

int $connectionType
int $options

query options

bool $cacheAffectedRows

whether to cache affected row

Return Value

mixed

bool tryMultiQuery(string $multiQuery = '', int $connectionType = Connection::TYPE_USER)

Send multiple SQL queries to the database server and execute the first one

Parameters

string $multiQuery

multi query statement to execute

int $connectionType

Return Value

bool

array getTables(string $database, int $connectionType = Connection::TYPE_USER)

returns array with table names for given db

Parameters

string $database

name of database

int $connectionType

Return Value

array

tables names

array getTablesFull(string $database, string|array $table = '', bool $tableIsGroup = false, int $limitOffset = 0, bool|int $limitCount = false, string $sortBy = 'Name', string $sortOrder = 'ASC', string|null $tableType = null, int $connectionType = Connection::TYPE_USER)

returns array of all tables in given db or dbs this function expects unquoted names: RIGHT: my_database WRONG: my_database WRONG: my_database if $tbl_is_group is true, $table is used as filter for table names

$dbi->getTablesFull('my_database');
$dbi->getTablesFull('my_database', 'my_table'));
$dbi->getTablesFull('my_database', 'my_tables_', true));

move into Table

Parameters

string $database database
string|array $table

table name(s)

bool $tableIsGroup

$table is a table group

int $limitOffset

zero-based offset for the count

bool|int $limitCount

number of tables to return

string $sortBy

table attribute to sort by

string $sortOrder

direction to sort (ASC or DESC)

string|null $tableType

whether table or view

int $connectionType

Return Value

array

list of tables in given db(s)

array getVirtualTables(string $db)

Get VIEWs in a particular database

Parameters

string $db

Database name to look in

Return Value

array

Set of VIEWs inside the database

array getDatabasesFull(string|null $database = null, bool $forceStats = false, int $connectionType = Connection::TYPE_USER, string $sortBy = 'SCHEMA_NAME', string $sortOrder = 'ASC', int $limitOffset = 0, bool|int $limitCount = false)

returns array with databases containing extended infos about them

move into ListDatabase?

Parameters

string|null $database database
bool $forceStats

retrieve stats also for MySQL < 5

int $connectionType
string $sortBy

column to order by

string $sortOrder

ASC or DESC

int $limitOffset

starting offset for LIMIT

bool|int $limitCount

row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']

Return Value

array

array getColumnMapFromSql(string $sqlQuery, array $viewColumns = [])

returns detailed array with all columns for sql

Parameters

string $sqlQuery

target SQL query to get columns

array $viewColumns

alias for columns

Return Value

array

array getColumnsFull(string|null $database = null, string|null $table = null, string|null $column = null, int $connectionType = Connection::TYPE_USER)

returns detailed array with all columns for given table in database, or all tables/databases

Parameters

string|null $database

name of database

string|null $table

name of table to retrieve columns from

string|null $column

name of specific column

int $connectionType

Return Value

array

array getColumn(string $database, string $table, string $column, bool $full = false, int $connectionType = Connection::TYPE_USER)

Returns description of a $column in given table

Parameters

string $database

name of database

string $table

name of table to retrieve columns from

string $column

name of column

bool $full

whether to return full info or only column names

int $connectionType

Return Value

array

flat array description

array getColumns(string $database, string $table, bool $full = false, int $connectionType = Connection::TYPE_USER)

Returns descriptions of columns in given table

Parameters

string $database

name of database

string $table

name of table to retrieve columns from

bool $full

whether to return full info or only column names

int $connectionType

Return Value

array

array indexed by column names

array getColumnNames(string $database, string $table, int $connectionType = Connection::TYPE_USER)

Returns all column names in given table

Parameters

string $database

name of database

string $table

name of table to retrieve columns from

int $connectionType

Return Value

array

array getTableIndexes(string $database, string $table, int $connectionType = Connection::TYPE_USER)

Returns indexes of a table

Parameters

string $database

name of database

string $table

name of the table whose indexes are to be retrieved

int $connectionType

Return Value

array

false|string|null getVariable(string $var, int $type = DatabaseInterface::GETVAR_SESSION, int $connectionType = Connection::TYPE_USER)

returns value of given mysql server variable

Parameters

string $var

mysql server variable name

int $type

DatabaseInterface::GETVAR_SESSION | DatabaseInterface::GETVAR_GLOBAL

int $connectionType

Return Value

false|string|null

value for mysql server variable

bool setVariable(string $var, string $value, int $connectionType = Connection::TYPE_USER)

Sets new value for a variable if it is different from the current value

Parameters

string $var

variable name

string $value

value to set

int $connectionType

Return Value

bool

void postConnect()

Function called just after a connection to the MySQL database server has been established. It sets the connection collation, and determines the version of MySQL which is running.

Return Value

void

void setCollation(string $collation)

Sets collation connection for user link

Parameters

string $collation

collation to set

Return Value

void

void postConnectControl(Relation $relation)

Function called just after a connection to the MySQL database server has been established. It sets the connection collation, and determines the version of MySQL which is running.

Parameters

Relation $relation

Return Value

void

string|false|null fetchValue(string $query, int|string $field = 0, int $connectionType = Connection::TYPE_USER)

returns a single value from the given result or query, if the query or the result has more than one row or field the first field of the first row is returned

$sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
$user_name = $dbi->fetchValue($sql);
// produces
// $user_name = 'John Doe'

Parameters

string $query

The query to execute

int|string $field

field to fetch the value from, starting at 0, with 0 being default

int $connectionType

Return Value

string|false|null

value of first field in first row from result or false if not found

array|null fetchSingleRow(string $query, string $type = DbalInterface::FETCH_ASSOC, int $connectionType = Connection::TYPE_USER)

Returns only the first row from the result or null if result is empty.

$sql = 'SELECT * FROM `user` WHERE `id` = 123';
$user = $dbi->fetchSingleRow($sql);
// produces
// $user = array('id' => 123, 'name' => 'John Doe')

Parameters

string $query

The query to execute

string $type

NUM|ASSOC returned array should either numeric associative or both

int $connectionType

Return Value

array|null

array fetchResult(string $query, string|int|array $key = null, string|int $value = null, int $connectionType = Connection::TYPE_USER)

returns all rows in the resultset in one array

$sql = 'SELECT * FROM `user`';
$users = $dbi->fetchResult($sql);
// produces
// $users[] = array('id' => 123, 'name' => 'John Doe')

$sql = 'SELECT `id`, `name` FROM `user`';
$users = $dbi->fetchResult($sql, 'id');
// produces
// $users['123'] = array('id' => 123, 'name' => 'John Doe')

$sql = 'SELECT `id`, `name` FROM `user`';
$users = $dbi->fetchResult($sql, 0);
// produces
// $users['123'] = array(0 => 123, 1 => 'John Doe')

$sql = 'SELECT `id`, `name` FROM `user`';
$users = $dbi->fetchResult($sql, 'id', 'name');
// or
$users = $dbi->fetchResult($sql, 0, 1);
// produces
// $users['123'] = 'John Doe'

$sql = 'SELECT `name` FROM `user`';
$users = $dbi->fetchResult($sql);
// produces
// $users[] = 'John Doe'

$sql = 'SELECT `group`, `name` FROM `user`'
$users = $dbi->fetchResult($sql, array('group', null), 'name');
// produces
// $users['admin'][] = 'John Doe'

$sql = 'SELECT `group`, `name` FROM `user`'
$users = $dbi->fetchResult($sql, array('group', 'name'), 'id');
// produces
// $users['admin']['John Doe'] = '123'

Parameters

string $query

query to execute

string|int|array $key

field-name or offset used as key for array or array of those

string|int $value

value-name or offset used as value for array

int $connectionType

Return Value

array

resultrows or values indexed by $key

array getCompatibilities()

Get supported SQL compatibility modes

Return Value

array

supported SQL compatibility modes

array getWarnings(int $connectionType = Connection::TYPE_USER)

returns warnings for last query

Parameters

int $connectionType

Return Value

array warnings

string getCurrentUser()

gets the current user with host

Return Value

string

the current user i.e. user@host

bool isSuperUser()

Checks if current user is superuser

Return Value

bool

bool isGrantUser()

No description

Return Value

bool

bool isCreateUser()

No description

Return Value

bool

bool isConnected()

No description

Return Value

bool

array getCurrentUserAndHost()

Get the current user and host

Return Value

array

array of username and hostname

Connection|null connect(int $mode, array|null $server = null, int|null $target = null)

Connects to the database server.

Parameters

int $mode

Connection mode.

array|null $server

Server information like host/port/socket/persistent

int|null $target

How to store connection link, defaults to $mode

Return Value

Connection|null

bool selectDb(string|DatabaseName $dbname, int $connectionType = Connection::TYPE_USER)

selects given database

Parameters

string|DatabaseName $dbname

database name to select

int $connectionType

Return Value

bool

bool moreResults(int $connectionType = Connection::TYPE_USER)

Check if there are any more query results from a multi query

Parameters

int $connectionType

Return Value

bool

bool nextResult(int $connectionType = Connection::TYPE_USER)

Prepare next result from multi_query

Parameters

int $connectionType

Return Value

bool

mixed storeResult(int $connectionType = Connection::TYPE_USER)

Store the result returned from multi query

Parameters

int $connectionType

Return Value

mixed

false when empty results / result set when not empty

string|bool getHostInfo(int $connectionType = Connection::TYPE_USER)

Returns a string representing the type of connection used

Parameters

int $connectionType

Return Value

string|bool

type of connection used

int|bool getProtoInfo(int $connectionType = Connection::TYPE_USER)

Returns the version of the MySQL protocol used

Parameters

int $connectionType

Return Value

int|bool

version of the MySQL protocol used

string getClientInfo()

returns a string that represents the client library version

Return Value

string

MySQL client library version

string getError(int $connectionType = Connection::TYPE_USER)

Returns last error message or an empty string if no errors occurred.

Parameters

int $connectionType

Return Value

string

string|int queryAndGetNumRows(string $query)

returns the number of rows returned by last query used with tryQuery as it accepts false

Parameters

string $query

query to run

Return Value

string|int

int insertId(int $connectionType = Connection::TYPE_USER)

returns last inserted auto_increment id for given $link or $GLOBALS['userlink']

Parameters

int $connectionType

Return Value

int

int|string affectedRows(int $connectionType = Connection::TYPE_USER, bool $getFromCache = true)

returns the number of rows affected by last query

Parameters

int $connectionType
bool $getFromCache

whether to retrieve from cache

Return Value

int|string

array getFieldsMeta(ResultInterface $result)

returns metainfo for fields in $result

Parameters

ResultInterface $result

result set identifier

Return Value

array

meta info for fields in $result

string quoteString(string $str, int $connectionType = Connection::TYPE_USER)

Returns properly quoted string for use in MySQL queries.

Parameters

string $str

string to be quoted

int $connectionType

Return Value

string

string escapeString(string $str, int $connectionType = Connection::TYPE_USER) deprecated

deprecated Use {@see \PhpMyAdmin\Dbal\quoteString()} instead.

returns properly escaped string for use in MySQL queries

Parameters

string $str

string to be escaped

int $connectionType

Return Value

string

a MySQL escaped string

string escapeMysqlWildcards(string $str)

Returns properly escaped string for use in MySQL LIKE clauses.

This method escapes only _, %, and /. It does not escape quotes or any other characters.

Parameters

string $str

string to be escaped

Return Value

string

a MySQL escaped LIKE string

bool isAmazonRds()

Checks if this database server is running on Amazon RDS.

Return Value

bool

string getKillQuery(int $process)

Gets SQL for killing a process.

Parameters

int $process

Process ID

Return Value

string

SystemDatabase getSystemDatabase()

Get the phpmyadmin database manager

Return Value

SystemDatabase

Table getTable(string $dbName, string $tableName)

Get a table with database name and table name

Parameters

string $dbName

DB name

string $tableName

Table name

Return Value

Table

string getDbCollation(string $db)

returns collation of given db

Parameters

string $db

name of db

Return Value

string

collation of $db

string getServerCollation()

returns default server collation from show variables

Return Value

string

int getVersion()

Server version as number

Return Value

int

string getVersionString()

Server version

Return Value

string

string getVersionComment()

Server version comment

Return Value

string

bool isMariaDB()

Whether connection is MariaDB

Return Value

bool

bool isPercona()

Whether connection is Percona

Return Value

bool

Statement|null prepare(string $query, int $connectionType = Connection::TYPE_USER)

Prepare an SQL statement for execution.

Parameters

string $query

The query, as a string.

int $connectionType

Return Value

Statement|null