laykith / easyconnect
A simple PDO manager to manage connections and reduce code in non-framework PHP scripts.
1.1.0
2018-11-19 08:27 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-11 20:06:37 UTC
README
A simple PDO manager to manage connections and reduce code in non-framework PHP scripts.
Tested:
- SQLite
- MySQL
- PostgreSQL
Setup:
- Install Composer and run
composer require laykith/easyconnect
in your project. - Start using EasyConnect
Recommended
- Add vlucas/phpdotenv with
composer require vlucas/phpdotenv
- Add your .env file to .gitignore
Usage:
Load class
require __DIR__ . '/vendor/autoload.php'; use EasyConnect\EasyConnect; //Example configs (Use what you need based on driver) $config = [ 'driver' => 'Sqlite', //MySQL SQLite pgsql - (Case Insensetive) 'filepath' => 'absolute/path/to/database/file.db', //required for: SQLite 'host' => 'localhost', //required for: MySQL & pgsql 'port' => '3306', //required for: MySQL & pgsql 'dbname' => 'name', //required for: MySQL & pgsql 'username' => 'root', //required for: MySQL & pgsql 'password' => 'password123', //required for: MySQL & pgsql ]; //Suggestion: Use vlucas/phpdotenv package to load values $config = [ 'driver' => getEnv('database_driver'), //MySQL SQLite pgsql - (Case Insensetive) 'filepath' => getEnv('database_filepath'), //required for: SQLite 'host' => getEnv('database_host'), //required for: MySQL & pgsql 'port' => getEnv('database_port'), //required for: MySQL & pgsql 'dbname' => getEnv('database_name'), //required for: MySQL & pgsql 'dbname' => getEnv('database_name'), //required for: MySQL & pgsql 'password' => getEnv('password'), //required for: MySQL & pgsql ]; $database = new EasyConnect($config);
Retrive data from database
//Example query $query = 'SELECT * FROM user'; //Params (Optional but highly advised to protect from SQL injections) $params = []; //Returns array of results $result = $database->getData($query, $params);
Inserting / Updating / Deleting data
//Insert data //Query (Required) $query = 'INSERT INTO user (username, password) VALUES (:username, :password)'; //Params (Optional but highly advised to protect from SQL injections) $params = [ ':username' => $_POST['username'], ':password' => password_hash($_POST['password'], PASSWORD_BCRYPT), ]; //Insert into Database $database->setData($query,$params);
Retrive PDO exceptions
//Gets the error message if PDO exception was triggered. $database->getError();