All database operations in ProcessWire are performed via this PDO-style database class.
ProcessWire creates the database connection automatically at boot and this is available from the $database
API variable. If you want to create a new connection on your own, choose either option A or B below:
// The following are required to construct a WireDatabasePDO
$dsn = 'mysql:dbname=mydb;host=myhost;port=3306';
$username = 'username';
$password = 'password';
$driver_options = []; // optional
// Construct option A
$db = new WireDatabasePDO($dsn, $username, $password, $driver_options);
// Construct option B
$db = new WireDatabasePDO([
'dsn' => $dsn,
'user' => $username,
'pass' => $password,
'options' => $driver_options, // optional
'reader' => [ // optional
'dsn' => '…',
…
],
…
]);
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the WireDatabasePDO
class also inherits all the methods and properties of: Wire.
Queries
Name | Return | Summary | |
---|---|---|---|
$database->exec() $database->exec($statement) $database->exec($statement, string $note = '') | bool int | Execute an SQL statement string | |
$database->execute() $database->execute(\PDOStatement $query) $database->execute(\PDOStatement $query, bool $throw = true, int $maxTries = 3) | bool | Execute a PDO statement, with retry and error handling | |
$database->lastInsertId() $database->lastInsertId() $database->lastInsertId($name = null) | string | Returns the ID of the last inserted row or sequence value | |
$database->prepare() $database->prepare(string $statement) $database->prepare(string $statement, $driver_options = [], string $note = '') | PDOStatement WireDatabasePDOStatement | Prepare an SQL statement for accepting bound parameters | |
$database->query() $database->query(string $statement) $database->query(string $statement, string $note = '') | PDOStatement | Executes an SQL statement, returning a result set as a PDOStatement object |
Transactions
Name | Return | Summary | |
---|---|---|---|
$database->allowTransaction() $database->allowTransaction() $database->allowTransaction(string $table = '') | bool | Allow a new transaction to begin right now? (i.e. supported and not already in one) | |
$database->beginTransaction() $database->beginTransaction() $database->beginTransaction() | bool | Initiates a transaction | |
$database->commit() $database->commit() $database->commit() | bool | Commits a transaction | |
$database->inTransaction() $database->inTransaction() $database->inTransaction() | bool | Checks if inside a transaction | |
$database->rollBack() $database->rollBack() $database->rollBack() | bool | Rolls back a transaction | |
$database->supportsTransaction() $database->supportsTransaction() $database->supportsTransaction(string $table = '') | bool | Are transactions available with current DB engine (or table)? |
Schema
Info
Sanitization
Connection
Name | Return | Summary | |
---|---|---|---|
$database->closeConnection() $database->closeConnection() $database->closeConnection() | (nothing) | Close the PDO connection | |
$database->errorCode() $database->errorCode() $database->errorCode() | string | Fetch the SQLSTATE associated with the last operation on the statement handle | |
$database->errorInfo() $database->errorInfo() $database->errorInfo() | array | Fetch extended error information associated with the last operation on the database handle | |
$database->getAttribute() $database->getAttribute(int $attribute) $database->getAttribute(int $attribute) | mixed | Retrieve a database connection attribute | |
$database->pdo() $database->pdo() $database->pdo($type = null) | PDO | Return the actual current PDO connection instance | |
$database->setAttribute() $database->setAttribute(int $attribute, mixed $value) $database->setAttribute(int $attribute, mixed $value) | bool | Sets an attribute on the database handle |
Custom
Name | Return | Summary | |
---|---|---|---|
$database->backups() $database->backups() $database->backups() | WireDatabaseBackup | Retrieve new instance of WireDatabaseBackups ready to use with this connection | |
$database->queryLog() $database->queryLog() $database->queryLog($sql = '', string $note = '') | array bool | Log a query, start/stop query logging, or return logged queries | |
$database->sqlMode() $database->sqlMode() $database->sqlMode(string $action = 'get', string $mode = '', string $minVersion = '', $pdo = null) | string bool | Get SQL mode, set SQL mode, add to existing SQL mode, or remove from existing SQL mode |
Additional methods and properties
In addition to the methods and properties above, WireDatabasePDO also inherits the methods and properties of these classes:
API reference based on ProcessWire core version 3.0.236