PHPackages                             jr-cologne/db-class - 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. [Database &amp; ORM](/categories/database)
4. /
5. jr-cologne/db-class

ActiveLibrary[Database &amp; ORM](/categories/database)

jr-cologne/db-class
===================

A simple database class with PHP, PDO and a query builder.

2.4.0(7y ago)11071MITPHPPHP &gt;=7.0

Since Jul 3Pushed 7y ago1 watchersCompare

[ Source](https://github.com/jr-cologne/db-class)[ Packagist](https://packagist.org/packages/jr-cologne/db-class)[ Docs](https://github.com/jr-cologne/db-class)[ RSS](/packages/jr-cologne-db-class/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)Dependencies (2)Versions (13)Used By (1)

db-class
========

[](#db-class)

[![Build Status](https://camo.githubusercontent.com/502a94017907c88a6719638ca92afd462faf6baf28f02e529f87c6375656ba8c/68747470733a2f2f7472617669732d63692e6f72672f6a722d636f6c6f676e652f64622d636c6173732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jr-cologne/db-class)

This project is a simple database class with PHP, PDO and a query builder.

The class extends PDO for more control and in order to keep all features of PDO.

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

[](#requirements)

- [PHP](http://php.net) (version 7.0 or higher)
- Database, which supports PDO (e.g. MySQL)

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

[](#installation)

If you want to use the database class for your own project, you have two options to install it:

### Using Composer (recommended)

[](#using-composer-recommended)

Once you have installed [Composer](https://getcomposer.org/), execute this command:

```
composer require jr-cologne/db-class

```

Then you just have to include the autoloader:

```
require_once 'vendor/autoload.php';
```

### Manual Installation

[](#manual-installation)

1. Download the ZIP file of this project
2. Unzip it and move everything to your own project directory.
3. Include all files of the database class into your project like that:

```
require_once 'path/to/db-class/src/DB.php';
require_once 'path/to/db-class/src/QueryBuilder.php';
require_once 'path/to/db-class/src/Exceptions/UnsupportedKeywordException.php';
```

Now you should be ready to start!

Basic Usage
-----------

[](#basic-usage)

### Namespace

[](#namespace)

Before instantiating the class, always make sure to use the right namespaces:

```
use JRCologne\Utils\Database\DB;
use JRCologne\Utils\Database\QueryBuilder;
```

### Instantiating Class

[](#instantiating-class)

To be able to use the class, you have to instantiate it.

Just do this:

```
$db = new DB(new QueryBuilder);
```

### Connecting to Database

[](#connecting-to-database)

You can connect to a database with the help of the method `DB::connect()`.

An simple example:

```
if ($db->connect('mysql:host=localhost;dbname=db-class-example;charset=utf8', 'root', 'root')) {
  echo 'Successfully connected to database';
} else {
  echo 'Connection failed';
}
```

### Checking Connection to Database

[](#checking-connection-to-database)

You can also check the connection to the database by the method `DB::connected()` after connecting.

Example:

```
if ($db->connected()) {
  echo 'Successfully connected to database';
} else {
  echo 'Connection failed';
}
```

### Retrieving Data from Database

[](#retrieving-data-from-database)

In order to retrieve data from a database, you need to walk through the following three steps:

1. Choose a table with the method `DB::table()`.
2. Select the data you want to retrieve.
3. Retrieve the selected data.

Fortunately, this is super simple with the database class:

```
$data = $db->table('users')->select('*')->retrieve();

if ($data === false) {
  echo 'Ops, something went wrong retrieving the data from the database!';
} else if (empty($data)) {
  echo 'It looks like there is no data in the database!';
} else {
  echo 'Successfully retrieved the data from the database!';

  echo '', print_r($data, true), '';
}
```

It will basically retrieve all records from the selected table.

### Inserting Data into Database

[](#inserting-data-into-database)

If you want to insert data into a database, you have two methods which you can use:

- `DB::insert()` (to insert one row of data)
- `DB::multi_insert()` (to insert multiple rows of data)

In this case, we are just going to insert one row.

The procedure is as follows:

1. Choose a table with the method `DB::table()`.
2. Insert the data with the method `DB::insert()`.

Example:

```
$inserted = $db->table('users')->insert('username, password', [
  'username' => 'test',
  'password' => 'password'
]);

if ($inserted) {
  echo 'Data has successfully been inserted';
} else if ($inserted === 0) {
  echo 'Ops, some data could not be inserted';
} else {
  echo 'Inserting of data is failed';
}
```

### Updating Data from Database

[](#updating-data-from-database)

In case you want to update data from a database, you can use the method `DB::update()`.

The following steps are required:

1. Choose a table with the method `DB::table()`.
2. Update the data with the method `DB::update()`.

Example:

```
if (
  $db->table('users')->update(
    [
      'username' => 'test123',  // new data
      'password' => 'password123',
    ],
    [
      'username' => 'test',    // where clause
      'password' => 'password',
    ]
  )
) {
  echo 'Data has successfully been updated';
} else {
  echo 'Updating data failed';
}
```

This will update the record(s) where the `username` is equal to `test` and the `password` is equal to `password` to `test123` for the `username` and `password123` for the `password`.

### Deleting Data from Database

[](#deleting-data-from-database)

In order to delete data from a database, follow these steps:

1. Choose a table with the method `DB::table()`.
2. Delete the data with the method `DB::delete()`.

Here's an simple example which deletes the record(s) where the `username` is equal to `test`:

```
if ($db->table('users')->delete([
  'username' => 'test'  // where clause
])) {
  echo 'Data has successfully been deleted';
} else {
  echo 'Deleting data failed';
}
```

### Custom Where Clauses

[](#custom-where-clauses)

#### Custom Logical Operators in Where Clause

[](#custom-logical-operators-in-where-clause)

Since the release of [version 2.3](https://github.com/jr-cologne/db-class/releases/tag/v2.3.0), a where clause can also have custom logical operators.

This is how a where clause with custom logical operators could look like when retrieving data from a database:

```
$data = $db->table('users')->select('*', [
  'id' => 1,
  '||',
  'username' => 'test'
])->retrieve();
```

#### Custom Comparison Operators in Where Clause

[](#custom-comparison-operators-in-where-clause)

Since the release of [version 2.4](https://github.com/jr-cologne/db-class/releases/tag/v2.4.0), a where clause can also have custom comparison operators.

This is how a where clause with custom comparison operators could look like when retrieving data from a database:

```
$data = $db->table('users')->select('*', [
  [
    'id',
    '>=',
    1
  ],
  'username' => 'test',
  [
    'password',
    '!=',
    'test123'
  ]
])->retrieve();
```

### Using PDO's functionality

[](#using-pdos-functionality)

Since the database class is extending PDO, you can use the whole functionality of PDO with this class as well.

Just connect to the database using the method `DB::connect()` and after that simply use everything as normal.

An quick example:

```
// include all files
require_once('vendor/autoload.php');

// use right namespaces
use JRCologne\Utils\Database\DB;
use JRCologne\Utils\Database\QueryBuilder;

// instantiate database class with query builder
$db = new DB(new QueryBuilder);

// connect to database
$db->connect('mysql:host=localhost;dbname=db-class-example;charset=utf8', 'root', 'root');

// prepare query like with PDO class
$stmt = $db->prepare("SELECT * FROM `users`");

// execute query
$stmt->execute();

// fetch all results
$results = $stmt->fetchAll();
```

### API

[](#api)

Looking for a complete overview of each class, property and method of this database class?

Just head over to the [`API.md`](https://github.com/jr-cologne/db-class/blob/master/src/API.md) file where you can find everything you need.

It is located in the source (`src`) folder.

Further Examples / Stuff for Testing
------------------------------------

[](#further-examples--stuff-for-testing)

You want to see further examples of using the database class or you just want to play around with it a little bit?

- You can find further examples in the file [`example/example.php`](https://github.com/jr-cologne/db-class/blob/master/example/example.php).
- To play around with the database class, you can use the database provided in the file [`example/db-class-example.sql`](https://github.com/jr-cologne/db-class/blob/master/example/db-class-example.sql). Just import it in your database client and you are ready to start!

Contributing
------------

[](#contributing)

Feel free to contribute to this project! Any kind of contribution is highly appreciated.

In case you have any questions regarding your contribution, do not hesitate to open an Issue.

Versioning
----------

[](#versioning)

This project is using the rules of semantic versioning (since version 2). For more information, visit [semver.org](http://semver.org/).

License
-------

[](#license)

This project is licensed under the [MIT License](https://github.com/jr-cologne/db-class/blob/master/LICENSE).

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~38 days

Recently: every ~23 days

Total

12

Last Release

2864d ago

Major Versions

1.0.5 → 2.0.02017-10-03

PHP version history (2 changes)1.0.3PHP ^7.0

1.0.4PHP &gt;=7.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/18489354?v=4)[JR Cologne](/maintainers/jr-cologne)[@jr-cologne](https://github.com/jr-cologne)

---

Top Contributors

[![jr-cologne](https://avatars.githubusercontent.com/u/18489354?v=4)](https://github.com/jr-cologne "jr-cologne (79 commits)")

---

Tags

databasedb-classpdophpquery-builderphpdatabasepdoquery builderdb-class

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jr-cologne-db-class/health.svg)

```
[![Health](https://phpackages.com/badges/jr-cologne-db-class/health.svg)](https://phpackages.com/packages/jr-cologne-db-class)
```

###  Alternatives

[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k23.1k](/packages/clouddueling-mysqldump-php)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1815.7k12](/packages/popphp-pop-db)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
