PHPackages                             chrisbaltazar/dbconn - 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. chrisbaltazar/dbconn

ActiveLibrary

chrisbaltazar/dbconn
====================

Easy library to perform common and fast DB queries for non-framework projects

2.0.3(5y ago)2103MITPHPPHP &gt;=7.0

Since Aug 3Pushed 5y ago2 watchersCompare

[ Source](https://github.com/chrisbaltazar/PHP-DBConn-Library)[ Packagist](https://packagist.org/packages/chrisbaltazar/dbconn)[ RSS](/packages/chrisbaltazar-dbconn/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (4)DependenciesVersions (5)Used By (0)

DBConn PHP
==========

[](#dbconn-php)

This is a useful library to handle common and fast operations on MysQL/MariaDB database engines, using Object notation an taking inspiration from Laravel Eloquent.

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

[](#installation)

Just install the library via composer running

```
composer require chrisbaltazar/dbconn
```

### Requirements

[](#requirements)

- PHP &gt;= 7.0

Initial config.
---------------

[](#initial-config)

All the necessary configuration is done inside the class constructor, so you all you need to do is have ready the needed CONSTANTS/ENV values before instantiating your DB object.

### The list of config fields are:

[](#the-list-of-config-fields-are)

- DB\_HOST (string)
- DB\_USER (string)
- DB\_PWD (string)
- DB\_NAME (string)
- DB\_DEBUG (bool)
- SESSION\_ID (string) *name of the variable into the application session to get the current user*
- TIME\_ZONE (string) *GMT+1*
- SUMMER\_TIME (bool) *autocalculate the time changing*
- CHARSET (string) *utf8*

### Additional config.

[](#additional-config)

By default the library also tries to handle the popular *tablestamps*

- updated\_by
- updated\_at
- deleted\_at

and fill in their values on every query, this is also configured into the `constructor` so every change you may need can be done extending the class and overriding this part.

> The most important thing to remember is that the `init` method MUST be called after doing the class configuration if you are extending the library

Usage
-----

[](#usage)

```
use chrisbaltazar/dbconn;

$db = new DBConn();
```

Examples
--------

[](#examples)

Getting a list of results from one single table...

```
$data = $db->from('tablename')->getArray();
```

Maybe a JSON would be more useful for more common cases

```
$data = $db->from('tablename')->getJSON();
```

Selecting only the fields you need in the consult, (separated by comma)

```
$data = $db->select('field1, field2, field3')
           ->from('tablename')
           ->getJSON();
```

Every time you need to debug your consult you can use the `getSQL` method at the end in order to get the query body to be executed before finish.

```
$data = $db->select('field1, field2, field3')
           ->from('tablename')
           ->getSQL();
```

Let's make a JOIN with more tables

```
$data = $db->select('field1, field2, field3', 'field4', 'field5')
           ->from('maintable')
           ->join('table2')
           ->join('table3', 1, 'LEFT')
           ->getJSON();

// In this case you can declare on the "join" statement the following:
// 1. Table name for join to
// 2. Index of target table in the tables array to join with,
// for this example we have 3 tables in total, counting the main source(from table)
// In this case the index 1 will point to table2 instead of the maintable  which is index o
// 3. The type of join, INNER by default
```

> by default the joins are done using the common table's id such as `id` and `foreign_id`

If you need to specify the ON clause for every JOIN you can use

```
$data = $db->select('field1, field2, field3', 'field4')
           ->from('maintable')
           ->join('table2')->on('local_id', 'foreign_id')
           ->getJSON();
```

Now we are separating the fields to extract from each table

```
$data = $db->select('field1, field2, field3', 'field4', 'field5')
           ->from('maintable')
           ->join('table2')
           ->join('table3', 1, 'LEFT')
           ->getJSON();
// This way, we are extracting fields 1, 2 and 3 from table 0 or maintable
// and field4 from the fisrt join, table1 in this case
// the same for field5 which will come from table3 in that order
```

How about adding an ORDER and GROUP clauses

```
$data = $db->select('field1, field2, field3')
           ->from('tablename')
           ->order('somefield1, somefield2')
           ->group('someotherfield')
           ->getJSON();

// Also you can specify the scope of the ORDER or GROUP
// using array notation and index declaration like:

->order([1 => 'table1_field', 0 => 'table0_field']);
```

What about "WHERE" clause? Let's see...

```
$data = $db->select(['field1, field2', 'field3', 'field4'])
           ->from('tablename')
           ->join(['othertablename', 0, 'other_id = main_id'])
           ->join(['anothertablename', 1, 'another_id = other_id', 'LEFT'])
           ->where(['table0field1 = somevalue, table0field2 = 0', 'table1field = somethingelse'])
           ->getJSON();

// The result of the above statement would be:
...where table0_name.field1 = 'somevalue'
     and table0_name.field2 = '0'
     and table1_name.field = 'someothervalue'
```

Using the SAVE method for INSERTS and UPDATES

```
$db->save('tablename', ['fieldname' => 'value'...], ['fieldname' => 'value']);
// Here, you can set an array of values to INSERT or UPDATE the table,
// which will be auto evaluated depending on the third parameter, the "where" part
```

Another using of this...

```
$db->save('tablename', $_POST, ['id' => $_POST['id']);
// You can see how can be more dynamic than previous using if you like
// In this case, maybe the 'id' could be present or not and the method will
// evaluate it as well.
```

Finally, DELETES...

```
$db->delete('tablename', $id);
// The DELETE method, will detect if you are passing only a numeric value as condition
// and use it with the table id automatically
$db->delete('tablename', ['field' => 'value', ...]);
// Or you can either set all the condition fields to make the delete
```

Please remember that you can set previously the "delete flags" in order to avoid a permanent deletion

License
-------

[](#license)

[MIT](https://choosealicense.com/licenses/mit/)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 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 ~59 days

Total

4

Last Release

1935d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8417691?v=4)[Christian Baltazar](/maintainers/chrisbaltazar)[@chrisbaltazar](https://github.com/chrisbaltazar)

---

Top Contributors

[![chrisbaltazar](https://avatars.githubusercontent.com/u/8417691?v=4)](https://github.com/chrisbaltazar "chrisbaltazar (5 commits)")

### Embed Badge

![Health badge](/badges/chrisbaltazar-dbconn/health.svg)

```
[![Health](https://phpackages.com/badges/chrisbaltazar-dbconn/health.svg)](https://phpackages.com/packages/chrisbaltazar-dbconn)
```

PHPackages © 2026

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