PHPackages                             lowyiyiu/ppmw - 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. lowyiyiu/ppmw

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

lowyiyiu/ppmw
=============

A modern, single-file, heavily typed PHP 8 PDO MySQL wrapper.

v1(2mo ago)00MITPHPPHP &gt;=8.1

Since May 10Pushed 2mo agoCompare

[ Source](https://github.com/lowyiyiu/PPMW)[ Packagist](https://packagist.org/packages/lowyiyiu/ppmw)[ RSS](/packages/lowyiyiu-ppmw/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

PPMW (PHP PDO MySQL Wrapper)
============================

[](#ppmw-php-pdo-mysql-wrapper)

A modern, single-file, heavily typed PHP 8.1+ PDO MySQL wrapper. Designed for simplicity, performance, and ease of use, with built-in PSR-3 logging support and a fluid query builder.

Features
--------

[](#features)

- **Zero Configuration Boilerplate:** Easy to set up and get running.
- **Fluent Query Builder:** Chainable methods for building complex SQL queries securely.
- **Prepared Statements Default:** 100% immune to SQL injection when using the query builder.
- **Memory Efficient:** Support for PHP Generators (`yield`) to process massive datasets.
- **PSR-3 Logging:** Automatically logs slow queries and database errors (bring your own logger).
- **Single File Drop-In:** Install via Composer or download as a standalone, dependency-free file.

---

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

[](#installation)

### Option 1: Via Composer (Recommended)

[](#option-1-via-composer-recommended)

```
composer require lowyiyiu/ppmw
```

### Option 2: Manual Include

[](#option-2-manual-include)

Download `src/Database.php` and include it directly in your script. It includes a built-in PSR-3 interface fallback, meaning it requires **zero** external dependencies.

```
require_once 'path/to/src/Database.php';
```

---

Initialization &amp; Configuration
----------------------------------

[](#initialization--configuration)

To start, instantiate the `Database` class with your connection array.

```
use LOWYIYIU\PPMW\Database;

$config = [
    'host'     => '127.0.0.1',  // Default: 127.0.0.1
    'port'     => 3306,         // Default: 3306
    'database' => 'my_app',
    'username' => 'root',
    'password' => 'secret',
    'charset'  => 'utf8mb4',    // Default: utf8mb4
    'prefix'   => 'app_'        // Optional: Auto-prepended to table names
];

// Basic instantiation
$db = new Database($config);

// Advanced instantiation with PSR-3 Logger and custom Slow Query Threshold (in seconds)
// $db = new Database($config, $myPsr3Logger, 1.5);
```

---

Retrieving Data
---------------

[](#retrieving-data)

PPMW offers a fluent query builder to fetch data easily.

### Fetching Multiple Rows (`get`)

[](#fetching-multiple-rows-get)

Returns an array of associative arrays.

```
$users = $db->from('users')->get();
```

### Fetching a Single Row (`getOne`)

[](#fetching-a-single-row-getone)

Applies a `LIMIT 1` and returns a single associative array, or `null` if no record is found.

```
$user = $db->from('users')->where('id', 1)->getOne();
```

### Fetching a Single Column Value (`getValue`)

[](#fetching-a-single-column-value-getvalue)

Fetches exactly one specific column from one row. Useful for counts or specific lookups.

```
// Returns the integer count
$total = $db->getValue('COUNT(*)', 'users');

// Returns the email string
$email = $db->from('users')->where('id', 1)->getValue('email');
```

### Fetching Large Datasets with Cursors (`getCursor`)

[](#fetching-large-datasets-with-cursors-getcursor)

If you need to process thousands of rows, use `getCursor()`. It uses PHP Generators to fetch one row into memory at a time, preventing memory exhaustion.

```
$users = $db->from('users')->where('status', 'active')->getCursor();

foreach ($users as $user) {
    // Process $user row by row
}
```

---

Query Builder Methods
---------------------

[](#query-builder-methods)

You can chain these methods together to build complex SQL statements before calling an execution method (`get`, `update`, `delete`, etc.).

### Select &amp; From

[](#select--from)

By default, PPMW selects `*`. You can specify columns using `select()`.

```
$db->select('id', 'name', 'email')
   ->from('users')
   ->get();

// You can also use an alias
$db->from('users', 'u')->get();
```

### Where Clauses

[](#where-clauses)

The `where` method is dynamic and handles operators and arrays automatically.

```
// Basic equality: WHERE status = 'active'
$db->from('users')->where('status', 'active');

// Custom operator: WHERE age > 18
$db->from('users')->where('age', '>', 18);

// IN clause: WHERE role IN ('admin', 'editor')
// PPMW automatically detects arrays and uses IN
$db->from('users')->where('role', ['admin', 'editor']);

// OR clauses
$db->from('users')
   ->where('status', 'active')
   ->orWhere('role', 'admin');
```

### Sorting &amp; Grouping

[](#sorting--grouping)

```
// ORDER BY created_at DESC
$db->from('users')->orderBy('created_at', 'DESC');

// GROUP BY role, status
$db->from('users')->groupBy('role', 'status');
```

### Limit &amp; Offset

[](#limit--offset)

```
// LIMIT 10 OFFSET 20
$db->from('users')->limit(10)->offset(20)->get();
```

### Joins

[](#joins)

PPMW automatically applies your table prefix to joined tables.

```
// INNER JOIN
$db->from('users')
   ->join('posts', 'users.id', '=', 'posts.user_id')
   ->get();

// LEFT JOIN
$db->from('users')
   ->leftJoin('profiles', 'users.id', '=', 'profiles.user_id')
   ->get();
```

---

Writing Data
------------

[](#writing-data)

### Inserting

[](#inserting)

Pass an associative array of column-value pairs.

```
// Returns boolean true/false
$success = $db->insert('users', [
    'name'  => 'John Doe',
    'email' => 'john@example.com'
]);

// Returns the newly created auto-incrementing ID
$newId = $db->insertGetId('users', [
    'name'  => 'Jane Doe',
    'email' => 'jane@example.com'
]);
```

### Updating

[](#updating)

Always chain a `where()` clause before calling `update()`, otherwise you will update every row in the table.

```
// Returns the number of affected rows
$affected = $db->where('id', 1)->update('users', [
    'status' => 'inactive'
]);
```

### Deleting

[](#deleting)

Like updating, ensure you chain a `where()` clause first.

```
// Returns the number of affected rows
$deleted = $db->where('status', 'banned')->delete('users');
```

---

Advanced Usage
--------------

[](#advanced-usage)

### Transactions

[](#transactions)

Safely execute multiple queries. If one fails, you can roll everything back.

```
try {
    $db->beginTransaction();

    $db->insert('users', ['name' => 'Alice']);
    $db->insert('profiles', ['user_id' => 1, 'bio' => 'Hello']);

    $db->commit();
} catch (\Exception $e) {
    $db->rollBack();
    // Handle error
}
```

### Raw Queries

[](#raw-queries)

If the query builder isn't enough, you can execute raw SQL with prepared bindings.

```
// For SELECT queries returning data
$results = $db->rawQuery(
    "SELECT * FROM app_users WHERE age > ? AND status = ?",
    [18, 'active']
);

// For execution queries (INSERT, UPDATE, DELETE, ALTER)
$success = $db->raw(
    "UPDATE app_users SET logins = logins + 1 WHERE id = ?",
    [1]
);
```

### Connection Management

[](#connection-management)

```
// Disconnect (closes PDO connection)
$db->disconnect();

// Ping the server (returns true if alive, automatically reconnects if "MySQL gone away")
$isAlive = $db->ping();

// Get the raw PDO instance for native PDO methods
$pdo = $db->getPdo();
```

### Debugging Utilities

[](#debugging-utilities)

```
// Get the last executed query string (with bindings populated for debugging)
$sql = $db->getLastQuery();

// Get the row count of the last executed statement
$count = $db->getRowCount();
```

---

Error Handling &amp; Exceptions
-------------------------------

[](#error-handling--exceptions)

PPMW throws specific exceptions that you can catch to handle errors gracefully:

- `LOWYIYIU\PPMW\DatabaseException`: The base exception for the library.
- `LOWYIYIU\PPMW\DatabaseConnectionException`: Thrown if the initial connection fails (e.g., bad credentials).
- `LOWYIYIU\PPMW\DatabaseQueryException`: Thrown if a query has a syntax error or fails to execute.

```
use LOWYIYIU\PPMW\DatabaseConnectionException;

try {
    $db = new Database($config);
} catch (DatabaseConnectionException $e) {
    echo "Could not connect to database: " . $e->getMessage();
}
```

License
-------

[](#license)

This project is licensed under the MIT License. See the `LICENSE` file for details.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance85

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity42

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

75d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10480968?v=4)[Low Yiyiu](/maintainers/lowyiyiu)[@lowyiyiu](https://github.com/lowyiyiu)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/lowyiyiu-ppmw/health.svg)

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

###  Alternatives

[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[kimai/kimai

Kimai - Time Tracking

4.8k9.0k1](/packages/kimai-kimai)[cycle/database

DBAL, schema introspection, migration and pagination

71777.8k63](/packages/cycle-database)[api-platform/metadata

API Resource-oriented metadata attributes and factories

295.0M223](/packages/api-platform-metadata)[symfony/ai-store

Low-level abstraction for storing and retrieving documents in a vector store.

22400.9k114](/packages/symfony-ai-store)[perplorm/perpl

Perpl is an improved and still maintained fork of Propel2, an open-source Object-Relational Mapping (ORM) for PHP.

2411.8k](/packages/perplorm-perpl)

PHPackages © 2026

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