$database API variable / WireDatabasePDO class

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.

Show $var?             Show args?        

Queries

Schema

NameReturnSummary 
$database->columnExists(string $table)
bool arrayDoes the given column exist in given table? 
$database->getCharset()
stringGet current database charset (lowercase) 
$database->getColumns(string $table)
arrayGet all columns from given table 
$database->getEngine()
stringGet current database engine (lowercase) 
$database->getIndexes(string $table)
arrayGet all indexes from given table 
$database->getMaxIndexLength()
intGet max length allowed for a fully indexed varchar column in ProcessWire 
$database->getPrimaryKey(string $table)
string arrayGet column(s) or info for given table’s primary key/index 
$database->getTables()
arrayGet array of all tables in this database. 
$database->indexExists(string $table, string $indexName)
bool arrayDoes table have an index with given name? 
$database->renameColumn(string $table, string $oldName, string $newName)
boolRename a table column without changing type 
$database->renameColumns(string $table, array $columns)
intRename table columns without changing type 
$database->tableExists(string $table)
boolDoes the given table exist in this database? 

Connection

NameReturnSummary 
$database->closeConnection()
(nothing)Close the PDO connection 
$database->errorCode()
stringFetch the SQLSTATE associated with the last operation on the statement handle 
$database->errorInfo()
arrayFetch extended error information associated with the last operation on the database handle 
$database->getAttribute(int $attribute)
mixedRetrieve a database connection attribute 
$database->pdo()
PDOReturn the actual current PDO connection instance 
$database->setAttribute(int $attribute, mixed $value)
boolSets an attribute on the database handle 

Custom

NameReturnSummary 
$database->backups()
WireDatabaseBackupRetrieve new instance of WireDatabaseBackups ready to use with this connection 
$database->queryLog()
array bool intLog a query, start/stop query logging, or return logged queries 
$database->sqlMode()
string boolGet 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.200

Twitter updates

  • Weekly update: Smooth rollout for ProcessWire 3.0.200 main/master, plus getting started on a new WP-to-PW site conversion and why they can be so rewarding— More
    20 May 2022
  • New main/master version of ProcessWire that has more than 220 commits, resolves more than 80 issues, adds numerous new features, performance improvements and optimizations, and consumes HALF the disk space of our previous release— More
    13 May 2022
  • Summary of weekly core updates for 22 April 2022 More
    22 April 2022

Latest news

  • ProcessWire Weekly #419
    In issue 419 of ProcessWire Weekly we'll check out this week's core updates, introduce a brand new recipe of the week, and more. Read on!
    Weekly.pw / 21 May 2022
  • ProcessWire 3.0.200 new master/main version
    This new main/master version has more than 220 commits, resolves more than 80 issues, adds numerous new features, performance improvements and optimizations, and consumes HALF the disk space of our previous release. This post covers all the details.
    Blog / 13 May 2022
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK