PHPackages                             mk/database-connection-handler - 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. mk/database-connection-handler

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

mk/database-connection-handler
==============================

PDO wrapper for using MySQL databases with less configuration and more ease

1.2.0(5y ago)0626MITPHPPHP &gt;=5.3.1

Since Feb 4Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Mo0812/DatabaseConnectionHandler)[ Packagist](https://packagist.org/packages/mk/database-connection-handler)[ RSS](/packages/mk-database-connection-handler/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)DependenciesVersions (8)Used By (0)

DatabaseConnectionHandler
=========================

[](#databaseconnectionhandler)

[![Codacy Badge](https://camo.githubusercontent.com/8a3a43891eceb1b8ea4e5aa1d91339a0e3d80d4a8caf0136a65ea6b089ada964/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6536633363646261633665633430346338633736306165396332393930303236)](https://www.codacy.com/app/Mo0812/DatabaseConnectionHandler?utm_source=github.com&utm_medium=referral&utm_content=Mo0812/DatabaseConnectionHandler&utm_campaign=Badge_Grade)[![Latest Stable Version](https://camo.githubusercontent.com/e13b2ec0c5a7512c5986adf5c0ae2359b0a6cb998e70b29b415e12e1f0a52fc8/68747470733a2f2f706f7365722e707567782e6f72672f6d6b2f64617461626173652d636f6e6e656374696f6e2d68616e646c65722f762f737461626c65)](https://packagist.org/packages/mk/database-connection-handler)[![Latest Unstable Version](https://camo.githubusercontent.com/a55daf1f68d9493d6faf03b17a2557cf8e0e965d2888b2e9975caf77385be140/68747470733a2f2f706f7365722e707567782e6f72672f6d6b2f64617461626173652d636f6e6e656374696f6e2d68616e646c65722f762f756e737461626c65)](https://packagist.org/packages/mk/database-connection-handler)[![License](https://camo.githubusercontent.com/ed14adbecc9634a0c6323e7910bab114fe562611230e33c96976c3daf12dbcdb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4d6f303831322f4d4b48616c2e737667)](https://img.shields.io/github/license/Mo0812/MKHal.svg)

DatabaseConnectionHandler is a simple PDO wrapper. The DatabaseConnectionHandler allows easy access to a MySQL database through PDO. It includes a singleton pattern which can be used to open up only one database connection throughout the whole project, so that your application not run into handling to many connections to your database. It also has a certain use pattern which is described below.

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

[](#requirements)

- PHP version 5.3.1 or higher
- MySQL database to connect to (for now)

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

[](#installation)

### Composer

[](#composer)

`composer require mk/database-connection-handler`

### Manually

[](#manually)

1. Just copy the included files into a folder in your project and include the `DatabaseConnectionHandler.php` wherever you want in your php files to use it.
2. To give the connection credentials for your own database please edit the file `DataBaseConnection.php` and fill out the placeholders with your own mysql server informations.

Roadmap / Specifications
------------------------

[](#roadmap--specifications)

- Uses namespaces
- PHP 7+ compatible
- Error handling with exceptions
- Use of `?` and `:param` syntax for prepared statements
- Feel free to raise an issue or more features

Usage
-----

[](#usage)

### Namespace

[](#namespace)

Use `\MK\DB` as namespace.

### Open up connection / create an instance

[](#open-up-connection--create-an-instance)

After including `DatabaseConnectionHandler.php` into a php file you need the get the current instance over the static function `getInstance()`. After that you are ready to query your database.

### Query the database

[](#query-the-database)

After creating an instance of the `DatabaseConnectionHandler` you can use the `query()` method to either fetch results or just query inserts or anything else.

The `query()` method has two parameters:

- *$query* is the SQL query string.
- *$arguments* is an array with certain arguments for the query string. This argument could also be left empty.

A query with parameters is build up like any query in PDO you use "?" to signal the DatabaseConnectionHandler that there is a matching argument for the placeholder in the *$arguments* array. So i.e.:

```
use \MK\DB;

$dbc_handler = DatabaseConnectionHandler::getInstance();

$searched_name = "Jack";
$searched_age = 21;

$result = $dbc_handler->query("INSERT INTO my_table (name, age) VALUES (?, ?);", array($searched_name, $searched_age));
```

This prepared statement behaviour also work as in pure PDO with named placeholders:

```
use \MK\DB;

$dbc_handler = DatabaseConnectionHandler::getInstance();

$searched_name = "Jack";
$searched_age = 21;

$result = $dbc_handler->query("INSERT INTO my_table (name, age) VALUES (:name, :age);", array(":name" => $searched_name, ":age" => $searched_age));
```

### Get results from your query

[](#get-results-from-your-query)

To get results for your query every `query()` method call returns a `DatabaseResult` object. This object holds all the queried data and also the selected rows of the query and the last insert id, if it's meaningful.

The `DatabaseResult` has the following methods:

- `getSelectedRows()`: Returns the found rows for a query if meaningful.
- `nextRow()`: Returns the next row of a query if meaningful. This row is an associated array with the queried columns as keys.
- `fetchAll()`: Instead of returning only the next row of the query this method returns all rows which are left packed in an array.
- `getLastInsertID()`: Returns the last insert id if meaningful.

Here is a simple example of how to use the DatabaseResult object:

```
use \MK\DB;

$dbc_handler = DatabaseConnectionHandler::getInstance();

$searched_name = "Jack"
$searched_age = 21

$result = $dbc_handler = $dbc_handler->query("INSERT INTO my_table (name, age) VALUES (?, ?);", array($searched_name, $searched_age));

//Last insert id
$last_insert_id = $result->getLastInsertID();

//Loop through result
$result2 = $dbc_handler->query("SELECT id, name, age FROM my_table;", array());
foreach($result2->fetchAll() as $row) {
    print_r($row);
}

//Get single (estimated) result
$result3 = $dbc_handler->query("SELECT id, name, age FROM my_table WHERE name = ?;", array($searched_name));
if($result3->getSelectedRows() > 0) { //Is there an result?
    $searched_row = $result3->nextRow();
}
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 90.5% 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 ~129 days

Recently: every ~193 days

Total

7

Last Release

1884d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/134c178d792d4442760905accc7bf50d6a1a5f0f02a7ddbde1786fe298b49123?d=identicon)[mojo101](/maintainers/mojo101)

---

Top Contributors

[![Mo0812](https://avatars.githubusercontent.com/u/1677918?v=4)](https://github.com/Mo0812 "Mo0812 (19 commits)")[![nkt-mka](https://avatars.githubusercontent.com/u/95299121?v=4)](https://github.com/nkt-mka "nkt-mka (2 commits)")

---

Tags

databasepdopdo-mysqlpdo-wrapperphp

### Embed Badge

![Health badge](/badges/mk-database-connection-handler/health.svg)

```
[![Health](https://phpackages.com/badges/mk-database-connection-handler/health.svg)](https://phpackages.com/packages/mk-database-connection-handler)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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