https://github.com/jahidulpabelislam/database
Simple wrapper for PDO.
https://github.com/jahidulpabelislam/database
database mysql pdo php
Last synced: about 2 months ago
JSON representation
Simple wrapper for PDO.
- Host: GitHub
- URL: https://github.com/jahidulpabelislam/database
- Owner: jahidulpabelislam
- License: gpl-3.0
- Created: 2020-01-22T21:46:12.000Z (over 5 years ago)
- Default Branch: 2.x
- Last Pushed: 2024-04-13T11:13:22.000Z (about 1 year ago)
- Last Synced: 2024-04-14T12:01:30.527Z (about 1 year ago)
- Topics: database, mysql, pdo, php
- Language: PHP
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Database
[](https://www.codefactor.io/repository/github/jahidulpabelislam/database)
[](https://packagist.org/packages/jpi/database)
[](https://packagist.org/packages/jpi/database)
[](https://packagist.org/packages/jpi/database)
[](https://packagist.org/packages/jpi/database)
Simple extension to PDO with some extra convenient methods.
## Installation
Use [Composer](https://getcomposer.org/)
```bash
$ composer require jpi/database
```## Usage
Extra Methods:
- `prep(string, array): PDOStatement`: when you want to bind some parameters to a query
- `run(string, array): PDOStatement`: when you bind some parameters to a query and want to execute it
- `selectAll(string, array): array`: for a `SELECT` query, returns a multidimensional array of all the rows found
- `selectFirst(string, array): array`: for a `SELECT` query that has `LIMIT 1`, returns an associative array of the first row found (if any)
- `getLastInsertedId: int|null`: helpful after a `INSERT` query, returns the ID of the newly inserted rowOverridden Methods:
- `exec(string, array): int`: for `INSERT`, `UPDATE` and `DELETE` queries, returns the number of rows affectedAll 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:
```php
$rows = $connection->selectAll("SELECT * FROM users;");/**
$rows = [
[
"id" => 1,
"first_name" => "Jahidul",
"last_name" => "Islam",
"email" => "[email protected]",
"password" => "password123",
...
],
[
"id" => 2,
"first_name" => "Test",
"last_name" => "Example",
"email" => "[email protected]",
"password" => "password123",
...
],
...
];
*/
```#### selectFirst:
```php
$row = $connection->selectFirst("SELECT * FROM users LIMIT 1;");/**
$row = [
"id" => 1,
"first_name" => "Jahidul",
"last_name" => "Islam",
"email" => "[email protected]",
"password" => "password",
...
];
*/
```#### exec:
```php
// 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" => "[email protected]",
"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](https://github.com/jahidulpabelislam/database/issues) or email [me @ jahidulpabelislam.com](mailto:[email protected]) :smirk:.
## Authors
- [Jahidul Pabel Islam](https://jahidulpabelislam.com/) [](mailto:[email protected])
## Licence
This module is licenced under the General Public Licence - see the [licence](LICENSE.md) file for details.