PHPackages                             atk14/dbmole - 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. atk14/dbmole

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

atk14/dbmole
============

Simple database abstraction layer for PostgreSQL, MySQL, Oracle and SQL Server

v1.2.4(2w ago)02.0k4MITPHPPHP &gt;=5.6.0

Since Aug 28Pushed 2w ago2 watchersCompare

[ Source](https://github.com/atk14/DbMole)[ Packagist](https://packagist.org/packages/atk14/dbmole)[ Docs](https://github.com/atk14/DbMole)[ RSS](/packages/atk14-dbmole/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (10)Versions (17)Used By (4)

DbMole
======

[](#dbmole)

[![Build Status](https://camo.githubusercontent.com/92009e16392d7bafdeaf0836d6decbdfe95642392beec63524e2bfbb1ec3641e/68747470733a2f2f6170702e7472617669732d63692e636f6d2f61746b31342f44624d6f6c652e7376673f6272616e63683d6d6173746572)](https://app.travis-ci.com/atk14/DbMole)

DbMole provides basic functionality with database (Postgresql, MySQL or Oracle).

Basic usage
-----------

[](#basic-usage)

At first, define the global function dbmole\_connection which returns connection to the database.

Only one Postgresql database is considered in this example.

```
function dbmole_connection($dbmole){
  return pg_connect("dbname=testing_database host=localhost user=test password=test123");
}

```

#### Instantiating

[](#instantiating)

```
$dbmole = PgMole::GetInstance();

```

#### Selecting rows

[](#selecting-rows)

```
$rows = $dbmole->selectRows("SELECT id,title,author FROM books");
foreach($rows as $row){
  echo $row["id"].": ".$row["title"]." (".$row["author"].")";
}

```

#### Iterating over rows

[](#iterating-over-rows)

For large result sets, use `iterateRows()` instead of `selectRows()`. It returns a generator and fetches rows one by one, keeping memory usage low.

```
foreach($dbmole->iterateRows("SELECT id,title,author FROM books") as $row){
  echo $row["id"].": ".$row["title"]." (".$row["author"].")";
}

```

Limit and offset are supported:

```
foreach($dbmole->iterateRows("SELECT * FROM employees",[],["limit" => 100, "offset" => 0]) as $row){
  // process $row
}

```

The `cache` option is not available for `iterateRows()`.

#### Selecting single row

[](#selecting-single-row)

```
$row = $dbmole->selectRow("SELECT id,title,author FROM books WHERE id=123");
var_dump($row); // ["id" => "123", "title" => "Book Title", "author" => "John Doe"]

```

#### Selecting single value

[](#selecting-single-value)

```
$amount_of_books = $dbmole->selectSingleValue("SELECT COUNT(*) FROM books");
// or better
$amount_of_books = $dbmole->selectInt("SELECT COUNT(*) FROM books");

```

For selecting single values, there are also methods:

- selectValue()
- selectInt()
- selectFloat()
- selectBool()
- selectString()

#### Safe binding of the query variables

[](#safe-binding-of-the-query-variables)

```
$rows = $dbmole->selectRows("SELECT id,title,author FROM books WHERE UPPER(title) LIKE UPPER(:search)",[":search" => "%Goodies%"]);
$row = $dbmole->selectRow("SELECT id,title,author FROM books WHERE id=:id",[":id" => 123]);
$dbmole->doQuery("UPDATE books SET title=:title, author=:author WHERE id=:id",[":id" => 123,":title" => "Good Reading", ":author" => "Samantha Doe"]);

```

Objects can be used as bind values. If the object has a `getId()` method, its return value is used; otherwise the object is cast to string.

```
$dbmole->selectRow("SELECT * FROM books WHERE id=:id",[":id" => $book]);
$dbmole->doQuery("UPDATE books SET author_id=:author WHERE id=:id",[":author" => $author, ":id" => $book]);

```

#### Limiting rows:

[](#limiting-rows)

```
$rows = $dbmole->selectRows("SELECT * FROM employees",[],["limit" => "10", "offset" => 0]);
$rows = $dbmole->selectRows("SELECT * FROM employees WHERE created_at>=:date",[":date" => "2020-01-01"],["limit" => "10", "offset" => 0]);

```

#### Working in transaction

[](#working-in-transaction)

```
$dbmole->begin();

// do something with $dbmole

$dbmole->commit();

```

#### Working in transaction, avoiding unnecessary database connections

[](#working-in-transaction-avoiding-unnecessary-database-connections)

```
$dbmole->begin(["execute_after_connecting" => true]);

// do something with $dbmole and sometimes do nothig

if($dbmole->isConnected()){
  $dbmole->commit();
}

```

#### Insering new record into a table

[](#insering-new-record-into-a-table)

```
$dbmole->insertIntoTable("books",[
  "id" => 123,
  "title" => "Nice Reading",
  "author" => "Brody Doe"
]);

```

#### Sequencies

[](#sequencies)

```
$next_id = $dbmole->selectSequenceNextval("seq_book");
$curr_id = $dbmole->selectSequenceCurrval("seq_book");

```

#### Error callback

[](#error-callback)

When an error occurs on SQL level, DbMole call the specified callback.

```
DbMole::RegisterErrorHandler("dbmole_error_handler");

function dbmole_error_handler($dbmole){
  echo "Dear visitor, unfortunately an error has occurred";
  $dbmole->sendErrorReportToEmail("admin@example.com");
  $dbmole->logErrorReport();
  exit(1);
}

```

#### Connecting to more databases

[](#connecting-to-more-databases)

Specify all connections in the dbmole\_connection function:

```
function dbmole_connection($dbmole){
  $database_type = $dbmole->getDatabaseType();
  $config = $dbmole->getConfigurationName();

  switch($database_type){
    case "postgresql":
      if($config=="default"){
        return pg_connect("dbname=testing_database host=localhost user=test password=test123");
      }
      if($config=="import"){
        return pg_connect("dbname=import_database host=localhost user=import password=import11122");
      }
      break;

    case "mysql":
      return mysqli_connect("127.0.0.1", "username", "password", "database", 3306);
      break;
  }
}

```

Instantiating:

```
$dbmole = PgMole::GetInstance(); // same as PgMole::GetInstance("default")
$dbmole_import = PgMole::GetInstance("import");
$dbmole_mysql = MysqlMole::GetInstance();

```

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

[](#installation)

Just use the Composer:

```
composer require atk14/dbmole

```

Testing
-------

[](#testing)

Install required dependencies for development:

```
composer update --dev

```

Run tests:

```
cd test
../vendor/bin/run_unit_tests

```

License
-------

[](#license)

DbMole is free software distributed [under the terms of the MIT license](http://www.opensource.org/licenses/mit-license)

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance97

Actively maintained with recent releases

Popularity20

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 93.2% 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 ~203 days

Recently: every ~8 days

Total

13

Last Release

15d ago

PHP version history (2 changes)v1.1.3PHP &gt;=5.3.0

v1.2.2PHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6304dffbd91d7a978f98632b0e4e30d662dcdb691daadb1388a58984e98faf5c?d=identicon)[yarri](/maintainers/yarri)

---

Top Contributors

[![yarri](https://avatars.githubusercontent.com/u/974278?v=4)](https://github.com/yarri "yarri (110 commits)")[![mysutka](https://avatars.githubusercontent.com/u/974669?v=4)](https://github.com/mysutka "mysutka (8 commits)")

### Embed Badge

![Health badge](/badges/atk14-dbmole/health.svg)

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

###  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)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

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

Reliese Components for Laravel Framework code generation.

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

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

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