function Connection::query
Same name in this branch
- 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
Same name in other branches
- 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
- 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
- 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::query()
- 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::query()
- 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::query()
- 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::query()
- 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.
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\DatabaseCode
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.