jpi / database
Simple extension to PDO
Installs: 1 584
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.1 || ^8.0
- ext-pdo: *
Requires (Dev)
- jpi/codestyles: ^1.0
README
Simple extension to PDO with some extra convenient methods.
Installation
Use Composer
$ composer require jpi/database
Usage
Extra Methods:
prep(string, array): PDOStatement
: when you want to bind some parameters to a queryrun(string, array): PDOStatement
: when you bind some parameters to a query and want to execute itselectAll(string, array): array
: for aSELECT
query, returns a multidimensional array of all the rows foundselectFirst(string, array): array
: for aSELECT
query that hasLIMIT 1
, returns an associative array of the first row found (if any)getLastInsertedId: int|null
: helpful after aINSERT
query, returns the ID of the newly inserted row
Overridden Methods:
exec(string, array): int
: forINSERT
,UPDATE
andDELETE
queries, returns the number of rows affected
All methods except getLastInsertedId
take the query as the first parameter (required), and an array of params to bind to the query (optional).
Examples:
(Assuming instance has been created and set to a variable named $connection
)
selectAll:
$rows = $connection->selectAll("SELECT * FROM users;"); /** $rows = [ [ "id" => 1, "first_name" => "Jahidul", "last_name" => "Islam", "email" => "jahidul@jahidulpabelislam.com", "password" => "password123", ... ], [ "id" => 2, "first_name" => "Test", "last_name" => "Example", "email" => "test@example.com", "password" => "password123", ... ], ... ]; */
selectFirst:
$row = $connection->selectFirst("SELECT * FROM users LIMIT 1;"); /** $row = [ "id" => 1, "first_name" => "Jahidul", "last_name" => "Islam", "email" => "jahidul@jahidulpabelislam.com", "password" => "password", ... ]; */
exec:
// INSERT $numberOfRowsAffected = $connection->exec( "INSERT INTO users (first_name, last_name, email, password) VALUES (:first_name, :last_name, :email, :password);", [ "first_name" => "Jahidul", "last_name" => "Islam", "email" => "jahidul@jahidulpabelislam.com", "password" => "password", ] ); // UPDATE $numberOfRowsAffected = $connection->exec( "UPDATE users SET first_name = :first_name WHERE id = :id;", [ "id" => 1, "first_name" => "Pabel", ] ); // DELETE $numberOfRowsAffected = $connection->exec("DELETE FROM users WHERE id = :id;", ["id" => 1]);
Support
If you found this library interesting or useful please spread the word about this library: share on your socials, star on GitHub, etc.
If you find any issues or have any feature requests, you can open a issue or email me @ jahidulpabelislam.com 😏.
Authors
Licence
This module is licenced under the General Public Licence - see the licence file for details.