PHPackages                             sentgine/db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. sentgine/db

ActiveLibrary

sentgine/db
===========

A lightweight PHP PDO-based query builder for simplified database interactions.

v1.0.3(1y ago)017MITPHPPHP ^8.1

Since Apr 15Pushed 1y ago2 watchersCompare

[ Source](https://github.com/sentgine/db)[ Packagist](https://packagist.org/packages/sentgine/db)[ RSS](/packages/sentgine-db/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)DependenciesVersions (9)Used By (0)

DB by Sentgine
==============

[](#db-by-sentgine)

[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE.md)[![Latest Stable Version](https://camo.githubusercontent.com/3eca907b011ea5c3a7bb831744247e09f74b76fde88e21bd971ddf35553a1b28/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73656e7467696e652f64622e737667)](https://packagist.org/sentgine/db)[![Total Downloads](https://camo.githubusercontent.com/d2ec2599b942f1464fd1f8e6f925cac4cdd8bc81ca168e692c600a379419e552/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73656e7467696e652f64622e737667)](https://packagist.org/packages/sentgine/db)

A standalone database query builder wrapper using PDO.

Features
--------

[](#features)

### Database Connection:

[](#database-connection)

- Establishes a database connection using PDO (PHP Data Objects) to interact with MySQL databases.
- Supports multiple database drivers including MySQL, PostgreSQL, and SQLite.
- Handles database connection errors and exceptions gracefully.

### Query Building:

[](#query-building)

- Provides fluent interface methods for building SQL queries, such as `select`, `join`, `where`, `orWhere`, `andWhere`, `limit`, and `offset`.
- Supports selecting specific columns or all columns (`*`) from a table.
- Allows joining tables with different types of joins (e.g., INNER JOIN, LEFT JOIN, RIGHT JOIN).
- Enables adding WHERE clauses with various operators (e.g., '=', '&gt;', '&lt;', '&gt;=', '&lt;=').

### Query Execution:

[](#query-execution)

- Executes SQL queries using prepared statements to prevent SQL injection attacks.
- Fetches query results as arrays of objects for easy manipulation.
- Handles PDO exceptions and provides error handling mechanisms.

### Data Manipulation:

[](#data-manipulation)

- Supports inserting data into tables with `insert` method, providing an associative array of column names and values.
- Allows updating data in tables with `update` method, providing new data as an associative array and conditions as an array of conditions.
- Enables deleting data from tables with `delete` method, providing conditions as an associative array of column names and values.

### Pagination:

[](#pagination)

- Implements pagination of query results using `paginate` method, allowing specification of the number of items per page and the current page number.
- Calculates pagination information including total items, total pages, and current page.
- Supports seamless integration with existing query builder methods for building paginated queries.

### Additional Utilities:

[](#additional-utilities)

- Provides a `getLastQuery` method to retrieve the last executed query for debugging purposes.
- Implements a `truncate` method to truncate tables, removing all rows.
- Offers a `raw` method for executing raw SQL queries when needed.

Requirements
------------

[](#requirements)

- PHP 8.0 or higher.

Installation
------------

[](#installation)

You can install the package via Composer by running the following command:

```
composer require sentgine/db:^1.0
```

Sample Usage of DB
------------------

[](#sample-usage-of-db)

### Basic Usage

[](#basic-usage)

```
