composer install tomwright/extended-pdo
ExtendedPDO implements the singleton design pattern using tomwright/singleton. For more information on how the following code works, see the documentation.
$db = ExtendedPDO::createConnection($dsn, $username, $password, 'my-main-db');
$db2 = ExtendedPDO::getInstance('my-main-db');
var_dump($db === $db2) // TRUE
// Returns an array of records
$db->queryAll('SELECT * FROM users WHERE username = :username', [':username' => 'Tom']);
// Returns the first record
$db->queryRow('SELECT * FROM users WHERE username = :username LIMIT 1', [':username' => 'Tom']);
You can set the return type of the dbQuery()
, queryAll()
and queryRow()
methods using $db->setReturnType($x)
where $x
is the return type you'd like to use.
Available return types are as follows:
ExtendedPDO::RETURN_TYPE_OBJECT
- Your results will be returned as objectsExtendedPDO::RETURN_TYPE_ASSOC
- Your results will be returned as associative arraysExtendedPDO::RETURN_TYPE_STMT
- The statement object will be returned directly
You can also set a return type of \PDO::FETCH_ASSOC
for example and it will override any of the above. This makes all of the standard PDO
fetch types usable.
ExtendedPDO also comes with it's own Query Builder.