PHPackages                             modularr/database - 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. modularr/database

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

modularr/database
=================

Simple Database Library

2.2.1(6y ago)04UNLICENSEPHP

Since Apr 8Pushed 6y agoCompare

[ Source](https://github.com/Modularr/Database)[ Packagist](https://packagist.org/packages/modularr/database)[ RSS](/packages/modularr-database/feed)WikiDiscussions master Synced 1mo ago

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

Database
========

[](#database)

An easy to use class for Database queries in PHP.

- [API](#api)
- [Connections](#connections)
- [Examples](#data-examples)
- [Install](#installation)

API
---

[](#api)

```
DB::connect($db='test',$pass='',$user='root',$host='localhost',$type='mysql');
DB::getPdo();
DB::setPdo($db);
DB::quote($string,$remove_quotes=false);
DB::query($query, $params = array());
DB::fetchAll($query);
DB::fetchAll_safe($query);
DB::fetch_assoc($query);
DB::fetch_safe_assoc($query);
DB::fetch_object($query);
DB::fetch_safe_object($query);
DB::num_rows($query);
```

See Below for usage.

Connections
-----------

[](#connections)

This class lets you connect to PDO whichever way you choose, This maximizes flexibility by letting you simply give us your already existing object, or by using our class as your primary Database package and asking for the PDO object if you need to pass it to other libraries.

### Connection Syntax

[](#connection-syntax)

You can connect using multiple syntax to make it easy to use, available both in the `__construct()` as well as `connect()`

You can either use an associative array or the default which uses reverse order to let you define the most important values first, and lets you default irrelevant values such as host or type to it's defaults ('mysql' and 'localhost')

### Via Instantiation

[](#via-instantiation)

```
$db = new Database($db='test',$pass='',$user='root',$host='localhost',$type='mysql'); # Default Syntax
$db = new Database(['host'=>$host,'dbname'=>$database,'user'=>$username,'pass'=>$password]); # Alternative Syntax
```

### Via Method

[](#via-method)

```
$db->connect(DB,PASS,USER,HOST); # Establish a Connection With PDO
```

### Via Existing PDO Object

[](#via-existing-pdo-object)

```
$db->setPdo($pdo); # Assign PDO Connection to the Database Class
```

Usage and Use Case
------------------

[](#usage-and-use-case)

A facade is optional but has all the same functionality of the main class.

Regular Use Case
================

[](#regular-use-case)

```
$db = new Database(DB,PASS,USER,HOST); # Establish a Connection
$query = $db->query("SELECT * FROM table");
while($item = $db->fetch_object($query))
{
    echo'#'.htmlspecialchars($item->id).': '.htmlspecialchars($item->name).'';
}
```

Facades Use Case
================

[](#facades-use-case)

### Create Facade from PDO Object

[](#create-facade-from-pdo-object)

```
$db = new Database(DB,PASS,USER,HOST); # Establish a Connection
DB::Facade($db); # Initiate Database object Facade
```

### Connect Via Facade

[](#connect-via-facade)

```
DB::connect('database','pass','user','host');
```

### Use Case

[](#use-case)

```
$query = DB::query("SELECT * FROM table");
while($item = DB::fetch_object($query))
{
    echo'#'.htmlspecialchars($item->id).': '.htmlspecialchars($item->name).'';
}
```

### Query

[](#query)

```
$query = DB::query("SELECT * FROM table WHERE id = ?", [$_GET['id']]);
```

This is a query with bind parameters. First argument is the statement, second argument is an array of parameters (optional)

Note: We passed the query into a variable for later re-use.

### Quote

[](#quote)

```
$quoted_string = DB::quote($_GET['id']);
```

```
# Remove Quotes after quoting, and right before output,
# giving you a similar string as mysql_real_escape_string
$quoted_string = DB::quote($_GET['id'], 1);
```

Escaping in PDO adds quotes around the escaped string, which is an issue if you try doing a **LIKE** query:

```
# Default Quote adds '' quotes around the field, forcing you to do:
DB::query("SELECT * FROM table WHERE field LIKE ?", ['%'.$input.'%']);
DB::query("SELECT * FROM table WHERE field LIKE ".DB::quote('%'.$input.'%'));

# Removed Quoting, quotes but removes added quotes
DB::query("SELECT * FROM table WHERE field LIKE '%".DB::quote($input,1)."%'";
```

PDO does not provide a way to turn off quotes around escaped strings so, we created a function that simply removes the quotes (first and last characters). This returns a string similar to the old [mysql\_real\_escape\_string](http://php.net/manual/en/function.mysql-real-escape-string.php) function.

Please note that this requires you to start adding quotes yourself. Escaping is the default when you bind parameters in PDO. As such, escaping is turned on by default as per the original function (passthrough).

### Fetch and **Safe Fetch**

[](#fetch-and-safe-fetch)

This is regular returned object. You still need to apply htmlspecialchars yourself.

```
$table = DB::fetch_object($query);
```

This is safe returned object. htmlspecialchars is applied to all the objects's properties.

```
$table = DB::fetch_safe_object($query);
```

### Num Rows

[](#num-rows)

```
DB::num_rows($query); # Equivalent of $pdo->rowCount();
```

### Data Examples

[](#data-examples)

```
# Loop Objects
while($entry = DB::fetch_safe_object($query))
{
	# Because of fetch_safe_object we don't need to apply htmlspecialchars
    echo ''.$entry->name.'';
}
# Single Object
$entry = DB::fetch_safe_object($query);
echo $entry->name;

# Loop Objects Using Foreach instead with Fetchall
foreach(DB::fetchAll_safe($query) as $entry)
{
	# Because of fetchAll_safe we don't need to apply htmlspecialchars
    echo ''.$entry->name.'';
}
# Single Object
$entry = DB::fetchAll_safe($query);
echo $entry[0]->name;
```

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

[](#installation)

via Composer:

```
composer require modularr/database

```

Or install like so:

```
{
    "require": {
        "modularr/database": "2.*"
    }
}
```

Manual:

1. Download [Release](https://github.com/Modularr/Database/releases) Or copy file manually
2. Include **Main.php** found under **src/** (this includes both **Database.php** and **Facade.php**)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity61

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

Total

7

Last Release

2222d ago

Major Versions

1.2.0 → 2.0.02020-04-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/f027c1ea2cde3c848dd3f0966bd5ea2c5331a850518260e878f7448fbb6555a9?d=identicon)[modbot](/maintainers/modbot)

---

Top Contributors

[![ISTPdev](https://avatars.githubusercontent.com/u/388467?v=4)](https://github.com/ISTPdev "ISTPdev (1 commits)")

---

Tags

composer-packagedatabasemysqlpdophpsqldatabasemysqlpdomodularr

### Embed Badge

![Health badge](/badges/modularr-database/health.svg)

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

###  Alternatives

[lincanbin/php-pdo-mysql-class

A PHP MySQL PDO class similar to the Python MySQLdb, which supports iterator and parameter binding when using 'WHERE IN' statement.

2386.4k](/packages/lincanbin-php-pdo-mysql-class)[sorskod/db

PDO wrapper. Extends PDO and PDOStatement with useful methods.

788.1k2](/packages/sorskod-db)

PHPackages © 2026

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