Connection
in package
Database Connection
Somewhere (probably in includes/easyparliament/init.php
) there should be
something like:
Class ParlDB extends \MySociety\TheyWorkForYou\Db\Connection {
function ParlDB() {
$this->init (OPTION_TWFY_DB_HOST, OPTION_TWFY_DB_USER, OPTION_TWFY_DB_PASS, OPTION_TWFY_DB_NAME);
}
}
Then, when you need to do queries, you do:
$db = new ParlDB;
$q = $db->query("SELECT haddock FROM fish");
$q
is then an instance of Db\Query
.
If other databases are needed, we just need to create a class for each, each
one extending Db\Connection
.
Call $db->display_total_duration()
at the end of a page to send total
query time to debug()
.
Depends on having the debug()
and getmicrotime()
functions available
elsewhere to output debugging info.
Table of Contents
Methods
- display_total_duration() : mixed
- Display Total Duration
- fatal_error() : mixed
- Fatal Error
- init() : bool
- Initialise Connection
- query() : Query
- Execute Query
- quote() : string
- Quote String
Methods
display_total_duration()
Display Total Duration
public
display_total_duration() : mixed
Displays the total time taken to execute all queries made via this connection.
fatal_error()
Fatal Error
public
fatal_error(string $error) : mixed
Display a fatal error and exit the script.
Parameters
- $error : string
-
The error message to display.
init()
Initialise Connection
public
init(string $db_host, string $db_user, string $db_pass, string $db_name) : bool
If an existing MySQL connection exists, use that. Otherwise, create a new connection.
Parameters
- $db_host : string
-
The hostname of the database server
- $db_user : string
-
The user to connect to the database as
- $db_pass : string
-
The password for the database user
- $db_name : string
-
The name of the database
Return values
bool —If the connection has been created successfully.
query()
Execute Query
public
query(string $sql[, array<string|int, mixed>|null $params = null ]) : Query
Takes a query, executes it and turns it into a query object.
Parameters
- $sql : string
-
The SQL query to execute
- $params : array<string|int, mixed>|null = null
-
Parameters to inject into the query
Return values
Query —An object containing the results of the query.
quote()
Quote String
public
quote(string $string) : string
Parameters
- $string : string
-
The string to quote.
Return values
string —The quoted string.