https://github.com/robclancy/db-connector
Fork of illuminate/database to provide a simple package for database connection alone
https://github.com/robclancy/db-connector
Last synced: 10 months ago
JSON representation
Fork of illuminate/database to provide a simple package for database connection alone
- Host: GitHub
- URL: https://github.com/robclancy/db-connector
- Owner: robclancy
- Created: 2013-04-11T00:31:15.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2013-04-11T07:53:50.000Z (about 13 years ago)
- Last Synced: 2025-03-15T07:38:08.795Z (about 1 year ago)
- Language: PHP
- Size: 344 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHP Database Connector
This package is simply a fork of [http://github.com/illuminate/database](illuminate/database) to just provide the connectors.
[](http://travis-ci.org/robclancy/db-connector)
## Installation
Add the following to the "require" section of your `composer.json` file:
```json
"robclancy/db-connector": "1.0.x"
```
## Basic Usage
You will need a config array, the following shows what can be used...
```php
$config = array(
'fetch' => PDO::FETCH_CLASS,
// SQLite
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
// MySQL
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
// Postgres SQL
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
// SQL Server
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
);
```
And then to make your connection...
```php
$connector = new Robbo\DbConnector\MysqlConnector;
$pdo = $connector->connect($config);
```
To make things a little easier and more flexible for applications that support multiple database types you can use a factory method to connect.
The config stays the same however you add a driver as well. For example...
```php
$config = array(
'driver' => 'mysql', // For other types this is 'pgsql', 'sqlite' or 'sqlsrv'
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
);
```
Then use the factor like so...
```php
$connector = Robbo\DbConnector\Connector::create($config); // Instance of Robbo/DbConnector/MySqlConnector
$pdo = $connector->connect($config);
// You can also have the factory connect for you by passing true as the second parameter, so...
$pdo = Robbo\DbConnector\Connector::create($config, true);
```