siteparts / pdoconnections
PDO数据库连接
v1.0.0
2023-08-23 16:22 UTC
Requires
- php: ^7.0 || ^8.0
Requires (Dev)
- phpstan/phpstan: ^1.10
Suggests
- psr/container: ^1.0 || ^2.0 to use the factory
This package is auto-updated.
Last update: 2024-09-23 18:52:26 UTC
README
在需要时创建PDO数据库连接。
安装
通过Composer
$ composer require siteparts/pdoconnections
使用方法
use PDO; use SiteParts\Database\Pdo\Connections; $dbConfig = [ 'default' => [ 'dsn' => 'mysql:dbname=foo;host=server1;charset=utf8', 'username' => 'john', 'password' => 'secret1', 'attributes' => [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ], ], 'bar' => [ 'dsn' => 'mysql:dbname=bar;host=server2;charset=utf8', 'username' => 'jack', 'password' => 'secret2', ], ]; $connections = new Connections($dbConfig); $defaultDb = $connections->getConnection(); // $defaultDb is PDO $barDb = $connections->getConnection("bar"); // $barDb is PDO // Subsequent calls return already existing connection $db = $connections->getConnection("bar"); // $db is the same as $barDb