https://github.com/phpgt/database
Database query organisation.
https://github.com/phpgt/database
database database-access database-api-organisation database-logic-organisation database-migrations php-library php-pdo phpgt sql sql-database sql-query
Last synced: 7 months ago
JSON representation
Database query organisation.
- Host: GitHub
- URL: https://github.com/phpgt/database
- Owner: phpgt
- License: mit
- Created: 2016-02-23T23:13:39.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2025-05-19T10:13:01.000Z (9 months ago)
- Last Synced: 2025-06-30T15:59:06.007Z (8 months ago)
- Topics: database, database-access, database-api-organisation, database-logic-organisation, database-migrations, php-library, php-pdo, phpgt, sql, sql-database, sql-query
- Language: PHP
- Homepage: https://php.gt/database
- Size: 757 KB
- Stars: 3
- Watchers: 2
- Forks: 3
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README

# Database API organisation.
Encloses your application's database scripts within a simple and standardised interface, separating database access from your application logic.
The first parameter to any database functions is always the query name, which represents a query file on disk - either a raw SQL file or a PHP representation of a query using [SqlBuilder][sqlbuilder].
***
## Example usage
This library organises SQL access through a consistent API. To execute an example query located at `src/query/user/getById.sql`, the following pattern is used:
```php
$userRow = $db->fetch("user/getById", 105);
```
Examples of CRUD operations:
```php
// "fetchAll" method returns an iterable ResultSet of Row objects.
$bookResultSet = $db->fetchAll("shopitem/getItemsInCategory", "books");
foreach($bookResultSet as $bookRow) {
echo "Book title: ", $bookRow->getString("title"), PHP_EOL;
echo "Book price: £", ($bookRow->getFloat("price") + $bookRow->getFloat("vat")), PHP_EOL;
if($bookRow->offerEnds) {
echo "Item on offer until: ", $bookRow->getDateTime("offerEnds")->format("dS M Y");
}
}
// "Create" method always returns the last inserted ID:
$newCustomerId = $db->create("customer/new", [
"first_name" => "Marissa",
"last_name" => "Mayer",
"dob" => new DateTime("1975-05-30"),
]);
// "Update" or "delete" methods always return the number of affected rows:
$numberOfItemsAffected = $db->update("shop/item/increasePrice", [
"percent" => 12.5,
"max_increase" => 20.00,
]);
$numberOfDeletedReviews = $db->delete(
"remove/deleteOlderThan",
new DateTime("-6 months")
);
// Individual type-safe fields can be pulled from queries that return only one column:
$userName = $db->fetchString("user/getUsernameById", 105);
```
## Features at a glance
+ [Automatic database migrations][wiki-migrations]
+ [Encapsulation of queries using `QueryCollection`s][wiki-query-collections]
+ [Bind parameters by name or sequentially][wiki-parameters]
+ [Fully configurable][wiki-config]
+ [Type safe getters][wiki-type-safety]
[sqlbuilder]: https://www.php.gt/sqlbuilder
[wiki-query-collections]: https://www.php.gt/docs/database/query-collections
[wiki-parameters]: https://www.php.gt/docs/database/parameters
[wiki-migrations]: https://www.php.gt/docs/database/migrations
[wiki-config]: https://www.php.gt/docs/database/config
[wiki-type-safety]: https://www.php.gt/docs/database/type-safety