PHPackages                             josemmo/umysql - 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. josemmo/umysql

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

josemmo/umysql
==============

Uncomplicated MySQL Abstraction Layer

v0.0.2(7mo ago)5787MITPHPPHP &gt;=7.1CI passing

Since Jul 5Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/josemmo/umysql)[ Packagist](https://packagist.org/packages/josemmo/umysql)[ Docs](https://github.com/josemmo/umysql)[ RSS](/packages/josemmo-umysql/feed)WikiDiscussions main Synced today

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

Uncomplicated MySQL
===================

[](#uncomplicated-mysql)

[![Build Status](https://github.com/josemmo/umysql/actions/workflows/ci.yml/badge.svg)](https://github.com/josemmo/umysql/actions)[![Latest Version](https://camo.githubusercontent.com/6c64bdb1f4b3b9f27936612fd54cc839c89ec70cf5c0eb4f5ea5c035f9878930/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f73656d6d6f2f756d7973716c)](https://packagist.org/packages/josemmo/umysql)[![Minimum PHP Version](https://camo.githubusercontent.com/0152b62ba34481d410c7b81bec7c94c058188f4bd69bef354bfcc4204718a060/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6a6f73656d6d6f2f756d7973716c)](#installation)[![License](https://camo.githubusercontent.com/9e2fda54f0b0bfe09cf85d2e4fab1411e7e4a3e72cb3979434760a482b796aa2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a6f73656d6d6f2f756d7973716c)](LICENSE)

UMySQL is an *extremely* simple PHP library for communicating with MySQL databases with ease while keeping overhead to a bare minimum. It aims to be an almost 1-to-1 and modern replacement for [SafeMySQL](https://github.com/colshrapnel/safemysql).

It doesn't provide any ORM, migration, events, caching, *etc.* functionality: Just the bare minimum to get you started.

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

[](#installation)

First of all, make sure your environment meets the following requirements:

- PHP 7.1 or higher
- [MySQLi extension](https://www.php.net/manual/en/mysqli.installation.php)

Then, you should be able to install this library using Composer:

```
composer require josemmo/umysql

```

Usage
-----

[](#usage)

### Creating a new instance

[](#creating-a-new-instance)

Typically, you'll want to create a new database instance using connection options:

```
$db = new UMySQL([
  'hostname' => '127.0.0.1', // Defaults to "localhost"
  'username' => 'app',       // Defaults to "root"
  'password' => 'S3cret',    // Defaults to "" (empty string)
  'database' => 'blog',      // Defaults to none selected
  'port'     => 3306,        // Defaults to 3306
  'charset'  => 'utf8mb4'    // Defaults to "utf8mb4"
]);
```

You can also connect to a UNIX socket:

```
$db = new UMySQL([
    'socket'   => '/run/mysqld/mysqld.sock',
    'username' => 'root',
    'password' => 'toor',
]);
```

As an alternative to options, you can wrap a `mysqli` instance around a database connection:

```
$db = new UMySQL(mysqli_connect('localhost', 'root', '', 'blog'));
```

### Writing queries

[](#writing-queries)

UMySQL supports various placeholders to safely replace values into queries:

- `?s` for strings, decimals and dates
- `?i` for integers
- `?n` for identifiers (table and column names)
- `?a` for arrays of strings
- `?u` for maps (associative arrays), useful in UPDATE queries
- `?p` for already parsed query parts

Here are some common examples on how to use them:

```
$db->parse('SELECT * FROM movies');
// SELECT * FROM movies

$db->parse('SELECT * FROM ?n WHERE username=?s AND points>=?i', 'users', 'nick', 100);
// SELECT * FROM `users` WHERE username='nick' AND points>=100

$db->parse('SELECT * FROM products WHERE id IN (?a)', [10, null, 30]);
// SELECT * FROM products WHERE id IN ('10', NULL, '30')

$db->parse('INSERT INTO metrics SET ?u', ['rtt' => 132.22, 'unit' => 'ms', 'verified' => 1]);
// INSERT INTO metrics SET `rtt`='132.22', `unit`='ms', `verified`=1

$db->parse('SELECT * FROM places WHERE city=?s ORDER BY ?n ?p', 'London', 'name', 'ASC');
// SELECT * FROM places WHERE city='London' ORDER BY `name` ASC
```

### Fetching results

[](#fetching-results)

The database instance comes with built-in helpers for retrieving rows from the database in a straightforward manner:

- `$db->getAll()` to get all rows in a result set
- `$db->getRow()` to get only the first row or `null` in case of an empty result set
- `$db->getCol()` to get the values from the first column of a result set
- `$db->getOne()` to get the first column from the first row or `false` in case of an empty result set

Some examples are:

```
$movies = $db->getAll('SELECT title, year FROM movies');
// [['title' => '...', 'year' => '...'], ['title' => '...', 'year' => '...'], ...]

$product = $db->getRow('SELECT * FROM products WHERE id=?i', 123);
// ['name' => '...', 'price' => '...']

$metrics = $db->getCol('SELECT rtt FROM metrics WHERE created_at>=?s', gmdate('Y-m-d 00:00:00'));
// ['112.12', '128.93', '120.66', '119.34', ...]

$userId = $db->getOne('SELECT id FROM users WHERE username=?s', 'some-username');
// '123'
```

### Executing other queries

[](#executing-other-queries)

For non-SELECT and more advanced queries, UMySQL has a `$db->query()` method that returns a custom `Result` instance.

Typically, you'll use this method when you don't care about the result of an operation or when there's no result set:

```
$db->query('TRUNCATE metrics');
// [\UMySQL\Result]
```

Result instances are also useful in UPDATE/DELETE operations to get the number of affected rows:

```
$affectedRows = $db->query('DELETE FROM users WHERE banned=1')->rowCount();
// '123'
```

Similarly, you can get the last insert ID of an auto-increment column in INSERT operations:

```
$productId = $db->query('INSERT INTO products (name, price) VALUES (?s, ?s)', 'Something', 12.34)->insertId();
// '321'
```

These instances can also be used to read a result set at your own pace:

```
$result = $db->query('SELECT * FROM large_table');
while ($row = $result->fetchRow()) {
    // Do something with `$row`
}
$result->free(); // Optional, will get called after `unset($result)`
```

Running the test suite
----------------------

[](#running-the-test-suite)

If you want to contribute to this project, please make sure to run the tests before committing new changes.

Tests are run against a MySQL database, so you'll need to define the following environment variables beforehand:

- `DB_HOSTNAME`
- `DB_USERNAME`
- `DB_PASSWORD` (optional)
- `DB_DATABASE`

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance64

Regular maintenance activity

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

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

Total

2

Last Release

220d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/35634d9b598826205bb05ac611f5e0b322a79a1fdec13168ca0019d584e87450?d=identicon)[josemmo](/maintainers/josemmo)

---

Top Contributors

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

---

Tags

databasemysqlmysqli

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/josemmo-umysql/health.svg)

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

###  Alternatives

[sergeytsalkov/meekrodb

The Simple PHP/MySQL Library

346410.5k13](/packages/sergeytsalkov-meekrodb)[ezsql/ezsql

Advance database access library. Make interacting with a database ridiculously easy. An universal interchangeable CRUD system.

86849.1k](/packages/ezsql-ezsql)[stefangabos/zebra_database

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

11712.6k](/packages/stefangabos-zebra-database)[go/db

Database library

6624.4k](/packages/go-db)[krugozor/database

PHP class library for simple, convenient, fast and safe work with MySql database, using PHP mysqli extension and imitation of prepared queries.

412.5k](/packages/krugozor-database)

PHPackages © 2026

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