function Connection::query

Same name in this branch
  1. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
Same name in other branches
  1. 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
  2. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::query()
  4. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::query()
  5. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
  6. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
  7. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()

Executes a query string against the database.

This method provides a central handler for the actual execution of every query. All queries executed by Drupal are executed as prepared statements.

Parameters

string $query: The query to execute. This is a string containing an SQL query with placeholders.

array $args: The associative array of arguments for the prepared statement.

array $options: An associative array of options to control how the query is run. The given options will be merged with self::defaultOptions(). See the documentation for self::defaultOptions() for details. Typically, $options['return'] will be set by a default or by a query builder, and should not be set by a user.

Return value

\Drupal\Core\Database\StatementInterface|null The executed statement.

Throws

\Drupal\Core\Database\DatabaseExceptionWrapper

\Drupal\Core\Database\IntegrityConstraintViolationException

\InvalidArgumentException

See also

\Drupal\Core\Database\Connection::defaultOptions()

8 calls to Connection::query()
Connection::attachDatabase in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Allows the connection to access additional databases.
Connection::hasJson in core/lib/Drupal/Core/Database/Connection.php
Runs a simple query to validate json datatype support.
Connection::query in core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
Executes a query string against the database.
Connection::queryRange in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Runs a limited-range query on this database object.
Connection::queryRange in core/modules/mysql/src/Driver/Database/mysql/Connection.php
Runs a limited-range query on this database object.

... See full list

1 method overrides Connection::query()
Connection::query in core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
Executes a query string against the database.

File

core/lib/Drupal/Core/Database/Connection.php, line 647

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

public function query($query, array $args = [], $options = []) {
    assert(is_string($query), 'The \'$query\' argument to ' . __METHOD__ . '() must be a string');
    assert(!isset($options['return']), 'Passing "return" option to query() has no effect. See https://www.drupal.org/node/3185520');
    assert(!isset($options['target']), 'Passing "target" option to query() has no effect. See https://www.drupal.org/node/2993033');
    // Use default values if not already set.
    $options += $this->defaultOptions();
    $this->expandArguments($query, $args);
    $statement = $this->prepareStatement($query, $options);
    try {
        $result = $statement->execute($args, $options);
    } catch (\Exception $e) {
        $this->exceptionHandler()
            ->handleExecutionException($e, $statement, $args, $options);
        $result = FALSE;
    }
    return $result ? $statement : NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.