PHPackages                             ezrarieben/pdo-wrapper-singleton - 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. ezrarieben/pdo-wrapper-singleton

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

ezrarieben/pdo-wrapper-singleton
================================

A wrapper class written for PDO MySQL DB connections following the singleton pattern

1.0.0(2y ago)08MITPHPPHP &gt;=8.1.0

Since Feb 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/ezrarieben/pdo-wrapper-singleton)[ Packagist](https://packagist.org/packages/ezrarieben/pdo-wrapper-singleton)[ Docs](https://github.com/ezrarieben/pdo-wrapper-singleton)[ RSS](/packages/ezrarieben-pdo-wrapper-singleton/feed)WikiDiscussions main Synced 1mo ago

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

pdo-wrapper-singleton
=====================

[](#pdo-wrapper-singleton)

A wrapper class written in PHP for PDO MySQL DB connections following the singleton pattern.

[![License: MIT](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://opensource.org/licenses/MIT)

Table of contents
=================

[](#table-of-contents)

- [pdo-wrapper-singleton](#pdo-wrapper-singleton)
- [Table of contents](#table-of-contents)
- [Installation](#installation)
- [Basic example](#basic-example)
- [Usage](#usage)
    - [Importing wrapper class](#importing-wrapper-class)
    - [Setting up DB connection](#setting-up-db-connection)
        - [Minimal setup example](#minimal-setup-example)
        - [Extensive setup example](#extensive-setup-example)
        - [Available parameters](#available-parameters)
    - [DB interaction](#db-interaction)
        - [Using PDO functions](#using-pdo-functions)
        - [Prepared statements](#prepared-statements)
            - [Example](#example)
            - [Example with named parameters](#example-with-named-parameters)
    - [Error handling](#error-handling)

Installation
============

[](#installation)

To use the wrapper in your project, add it as a dependency via composer:

```
composer require ezrarieben/pdo-wrapper-singleton

```

Basic example
=============

[](#basic-example)

```
use \ezrarieben\PdoWrapperSingleton\Database;

Database::setHost("localhost");
Database::setUser("user");
Database::setPassword("123456");
Database::setDbName("foobar");

try {
    $query = "SELECT * FROM `cars` WHERE `color` = ?";
    $stmt = Database::run($query, ['red']);
    $row = $stmt->fetch();
} catch (\PDOException $e) {
    die("PDO ERROR: " . $e->getMessage());
}
```

Usage
=====

[](#usage)

Importing wrapper class
-----------------------

[](#importing-wrapper-class)

For ease of use it is recommended to import the wrapper class with `use`

```
use \ezrarieben\PdoWrapperSingleton\Database;
```

Setting up DB connection
------------------------

[](#setting-up-db-connection)

In order to use the wrapper class `Database` the connection parameters need to be set first.

There are certain required parameters that need to be set in order for the PDO connection to work.
(See "Required" column in [available parameters table](#available-parameters) for required parameters).

#### Minimal setup example

[](#minimal-setup-example)

```
Database::setHost("localhost");
Database::setUser("user");
Database::setPassword("123456");
```

#### Extensive setup example

[](#extensive-setup-example)

```
Database::setHost("localhost");
Database::setPort(3307);
Database::setUser("user");
Database::setPassword("123456");
Database::setDbName("foobar");
Database::setPdoAttributes(array(
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
));
```

#### Available parameters

[](#available-parameters)

DescriptionSetter functionParametersRequiredDB server host`setHost()``string` $host**YES**User`setUser()``string` $user**YES**Password`setPassword()``string` $password**YES**DB server host`setPort()``int` $portDatabase name`setDbName()``?string` $dbNamePDO attributes`setPdoAttributes()``array` $attributesDB interaction
--------------

[](#db-interaction)

### Using PDO functions

[](#using-pdo-functions)

All PDO functions can be accessed statically through the `Database` class:

```
$query = "SELECT * FROM `cars` WHERE `color` = ?";
$stmt = Database::prepare($query);
$stmt->execute(['red']);
$row = $stmt->fetch();
```

### Prepared statements

[](#prepared-statements)

The `Database` class has a shortcut function for prepared statements called `run()`:

ParametersDescriptionRequired`string` $querySQL query to execute**YES**`array` $paramsparameters to pass to queryThe function returns a `PDOStatement` object if preperation and execution of query was successfull.
If preperation or execution of query failed the function will throw a `PDOException` or return `false` depending on the currently set PDO error mode.
(see: [Error handling](#error-handling) for more info)

#### Example

[](#example)

```
$query = "SELECT * FROM `cars` WHERE `color` = ?";
$stmt = Database::run($query, ['red']);
$row = $stmt->fetch();
```

#### Example with named parameters

[](#example-with-named-parameters)

```
$query = "SELECT * FROM `cars` WHERE `color` = :color";
$stmt = Database::run($query, [':color' => 'red']);
$row = $stmt->fetch();
```

Error handling
--------------

[](#error-handling)

PDO's [error mode](https://www.php.net/manual/en/pdo.error-handling.php) is set to `ERRMODE_EXCEPTION` by default.
Error handling can therefore be done through try and catch blocks.

```
try {
    $query = "SELECT * FROM `cars` WHERE `color` = ?";
    $stmt = Database::run($query, ['red']);
    $row = $stmt->fetch();
} catch (PDOException $e) {
    // Handle exception
}
```

When switching to a different [error mode](https://www.php.net/manual/en/pdo.error-handling.php) you will need to handle errors through booleans.

> **NOTE:** Error handling using booleans is only supported if you change PDO's error mode.

```
$query = "SELECT * FROM `cars` WHERE `color` = :color";
if($stmt = Database::run($query, [':color' => 'red'])) {
    // Preparing and executing statement was successfull so fetch the result
    $row = $stmt->fetch();
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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

Unknown

Total

1

Last Release

818d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a6b851d0aea026aade409a6efb6474bfbce9d83f9804cbae5ca605e46960780?d=identicon)[ezrarieben](/maintainers/ezrarieben)

---

Top Contributors

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

---

Tags

databasedatabasesmysqlmysql-databasepdopdo-mysqlpdo-wrapperphpwrapperwrapper-classphpdatabasemysqlpdowrapperwrapper classpdo\_mysqlpdo-wrapper

### Embed Badge

![Health badge](/badges/ezrarieben-pdo-wrapper-singleton/health.svg)

```
[![Health](https://phpackages.com/badges/ezrarieben-pdo-wrapper-singleton/health.svg)](https://phpackages.com/packages/ezrarieben-pdo-wrapper-singleton)
```

###  Alternatives

[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k5.5M69](/packages/ifsnop-mysqldump-php)[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k22.9k](/packages/clouddueling-mysqldump-php)[stefangabos/zebra_database

An advanced, compact and lightweight MySQL database wrapper library, built around PHP's MySQLi extension.

11812.0k](/packages/stefangabos-zebra-database)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1814.6k11](/packages/popphp-pop-db)[riverside/php-orm

PHP ORM micro-library and query builder

111.2k](/packages/riverside-php-orm)

PHPackages © 2026

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