FaaPz\PDO\Database extends PDO
The Database class is an extension of PHP's PDO object that provides additional factory style methods for the query building statements supported by this library. Because the Database object extends PDO, all PDO methods like ::beginTransaction(), ::rollBack() and ::commit() are fully supported.
All the same constructor parameters you would use to create a PDO object. The Database class will apply the following default options unless explicitly overridden:
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
Parameter | Description |
---|---|
$dsn |
Data source name |
$username |
Database username |
$password |
Database password |
$options |
Array of key => value connection options |
use FaaPz\PDO\Database;
$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
Creates a new Call statement that will use the optional METHOD parameter.
Parameter | Description |
---|---|
$procedure |
Optional procedure to call |
use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;
$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
// CALL MyProcedure(?, ?)
$statement = $database->call(new Method("MyProcedure", 1, 2));
$statement->execute();
Creates a new INSERT statement that will use the optional array of columns.
Parameter | Description |
---|---|
$columns |
Optional columns to use |
use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;
$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
// INSERT INTO table (col1, col2) VALUES (?, ?), (?, ?)
$statement = $database->insert(['col1', 'col2'])
->into('table')
->values(1, 2)
->values(3, 4);
$statement->execute();
Creates a new Select statement that will use the optional array of columns.
Parameter | Description |
---|---|
$columns |
Optional columns to select |
use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;
$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
// SELECT COUNT(id) AS cnt FROM table
$statement = $database->select(['cnt' => 'COUNT(id)'])
->from('table');
$statement->execute();
Creates a new UPDATE statement that will use the optional array of column / value pairs.
Parameter | Description |
---|---|
$pairs |
Optional column / value pairs to update |
use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;
$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
// UPDATE table SET col1 = ?, col2 = ?
$statement = $database->update(['col1' => 1, 'col2' => 2])
->table('table');
$statement->execute();
Creates a new DELETE statement that will use the optional table to delete from.
Parameter | Description |
---|---|
$table |
Optional table to delete from |
use FaaPz\PDO\Clause\Method;
use FaaPz\PDO\Database;
$database = new Database('mysql:host=localhost;dbname=test_db;charset=UTF8');
// DELETE FROM table
$statement = $database->delete('table');
$statement->execute();