PHPackages                             stefangabos/zebra\_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. stefangabos/zebra\_database

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

stefangabos/zebra\_database
===========================

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

3.0.0(2w ago)11812.8k↓61.7%37LGPL-3.0-or-laterPHPPHP &gt;=7.3.0CI passing

Since Aug 24Pushed 6mo ago21 watchersCompare

[ Source](https://github.com/stefangabos/Zebra_Database)[ Packagist](https://packagist.org/packages/stefangabos/zebra_database)[ Docs](https://github.com/stefangabos/Zebra_Database)[ GitHub Sponsors](https://github.com/stefangabos)[ RSS](/packages/stefangabos-zebra-database/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)Dependencies (5)Versions (35)Used By (0)

[![zebrajs](https://github.com/stefangabos/zebrajs/raw/master/docs/images/logo.png)](https://github.com/stefangabos/zebrajs/blob/master/docs/images/logo.png)

Zebra Database [![Tweet](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?text=A+compact,+lightweight+and+feature-rich+PHP+MySQLi+database+wrapper&url=https://github.com/stefangabos/Zebra_Database&via=stefangabos&hashtags=php,mysqli,mysql)
============================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#zebra-database-)

*A PHP MySQLi database wrapper that's lightweight, secure and feature-rich, providing methods for interacting with MySQL databases that are more secure, powerful and intuitive than PHP's default ones.*

[![Latest Stable Version](https://camo.githubusercontent.com/d3177b5931f99a7fc3660937f9242b56429de41aa50987b7238e6f30ea2054fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73746566616e6761626f732f7a656272615f6461746162617365)](https://packagist.org/packages/stefangabos/zebra_database) [![Total Downloads](https://camo.githubusercontent.com/34b90f36945e1aafb0991328d86c62ecde7480d40c02118d805277773cb78675/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746566616e6761626f732f7a656272615f6461746162617365)](https://packagist.org/packages/stefangabos/zebra_database) [![Monthly Downloads](https://camo.githubusercontent.com/c90555efc18437f9d2c4ba40e7d1b928bbce835c40701d59b2be373d5ff589f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f73746566616e6761626f732f7a656272615f6461746162617365)](https://packagist.org/packages/stefangabos/zebra_database) [![Tests](https://camo.githubusercontent.com/b896395f4ccb674ec33197254c514d160f13d64757fb1c4dd5b26e6eeff04a7a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73746566616e6761626f732f5a656272615f44617461626173652f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473)](https://github.com/stefangabos/Zebra_Database/actions/workflows/tests.yml) [![PHP](https://camo.githubusercontent.com/f8b7e810b0998d71a92003bf9e248235158a5a3e9c2874cbe6e6c037061b3d45/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f73746566616e6761626f732f7a656272615f64617461626173652f706870)](https://packagist.org/packages/stefangabos/zebra_database) [![License](https://camo.githubusercontent.com/22aef142fa359d3363ddcad2b4dbf34624813253303013558e89e34d49474e98/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73746566616e6761626f732f7a656272615f6461746162617365)](https://packagist.org/packages/stefangabos/zebra_database)

Zebra\_Database supports [transactions](https://dev.mysql.com/doc/refman/8.0/en/glossary.html#glos_transaction) and provides ways for caching query results either by saving cached data to the disk, in the session, by using [memcache](https://memcached.org/) or [redis](https://redis.io/).

The library provides a comprehensive debugging interface with detailed information about the executed queries: execution time, returned/affected rows, excerpts of the found rows, error messages, backtrace information, etc. It can also automatically [EXPLAIN](https://dev.mysql.com/doc/refman/8.0/en/explain.html) SELECT queries *(so you don't miss those keys again!)*.

Provides debugging information when called from **CLI** (command-line interface) and supports logging queries done through **AJAX** requests.

It encourages developers to write maintainable code and provides a better default security layer through *parameterized queries*, where values are given separately from the SQL and are automatically [escaped](https://www.php.net/manual/en/mysqli.real-escape-string.php) and quoted for you.

The code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to E\_ALL.

> 🔬 See the [debugging console](https://stefangabos.github.io/Zebra_Database/examples/index.html) in action

Key Features of this PHP MySQLi Wrapper
---------------------------------------

[](#key-features-of-this-php-mysqli-wrapper)

- it uses the [mysqli extension](https://www.php.net/manual/en/book.mysqli.php) extension for communicating with the database instead of the old *mysql* extension, which is officially deprecated as of PHP v5.5.0 and will be removed in the future; **this is not a wrapper for the PDO extension which is already a wrapper in itself!**
- can provide debugging information even when called from **CLI** (command-line interface)
- logs all queries, including those run through **AJAX**
- offers [lots of powerful methods](https://stefangabos.github.io/Zebra_Database/Zebra_Database/Zebra_Database.html) for easier interaction with MySQL
- supports [unbuffered queries](https://www.php.net/manual/en/mysqlinfo.concepts.buffering.php)
- provides a better security layer through *parameterized queries*, where values are given separately from the SQL and are automatically escaped and quoted
- provides a very detailed debugging interface with lots of useful information about executed queries; it also automatically [EXPLAIN](https://dev.mysql.com/doc/refman/8.0/en/explain.html)s each SELECT query
- supports caching of query results to the disk, in the session, or to a [memcache](https://memcached.org/) or to a [redis](https://redis.io/) server
- has [really good documentation](https://stefangabos.github.io/Zebra_Database/Zebra_Database/Zebra_Database.html)
- code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to **E\_ALL**

[![zebra database debugging console showing query details](https://raw.githubusercontent.com/stefangabos/Zebra_Database/master/docs/media/zebra-database-debug-console.png)](https://raw.githubusercontent.com/stefangabos/Zebra_Database/master/docs/media/zebra-database-debug-console.png)

📔 Documentation
---------------

[](#notebook_with_decorative_cover-documentation)

Check out the [comprehensive PHP MySQLi wrapper documentation](https://stefangabos.github.io/Zebra_Database/Zebra_Database/Zebra_Database.html).

🎂 Support the development of this project
-----------------------------------------

[](#-support-the-development-of-this-project)

Your support is greatly appreciated and it keeps me motivated continue working on open source projects. If you enjoy this project please star it by clicking on the star button at the top of the page. If you're feeling generous, you can also buy me a coffee through PayPal or become a sponsor. **Thank you for your support!** 🎉

[![Star it on GitHub](https://camo.githubusercontent.com/7e5d652d0be7fb0bc961e77ada29529e96225598da7d5b57d012f90d9f2057dc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f73746566616e6761626f732f7a656272615f64617461626173653f636f6c6f723d677265656e266c6162656c3d7374617225323069742532306f6e253230476974487562)](https://github.com/stefangabos/Zebra_Database) [![Donate](https://camo.githubusercontent.com/604e3db9c8751116b3f765aad0353ec7ded655bbe8aaacbc38d8c4a6b784b3ed/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d50617950616c2d677265656e2e737667)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3WTC6RTC7496Q) [![](https://camo.githubusercontent.com/7a6725dd6857bfabaca4b6783ac9ff3c2e882456681b5fdfb1fae916c0461c73/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d53706f6e736f722d6661666266633f6c6f676f3d47697448756225323053706f6e736f7273)](https://github.com/sponsors/stefangabos)

Requirements
------------

[](#requirements)

PHP 7.4.0+ with the **mysqli extension** activated, MySQL 4.1.22+

> The library itself still runs on PHP 5.6.40 and newer, and this is verified on every change with [PHPCompatibility](https://github.com/PHPCompatibility/PHPCompatibility) via `composer check-compat-legacy`, and by linting and smoke-testing it on a real PHP 5.6.40 via `tests/run-legacy.sh`. The supported version is given as 7.4 because that is the oldest version the test suite runs on, and *supported* should mean *tested*. If you are on PHP 5.6.40 to 7.3 the library will very likely work, but you are on your own.

For using **memcache** as caching method, PHP must have the [memcache](https://pecl.php.net/package/memcache) extension installed. The [memcache\_compressed](https://stefangabos.github.io/Zebra_Database/Zebra_Database/Zebra_Database.html#var$memcache_compressed) property, which hands the compressing over to memcache instead of doing it in the library, additionally requires that extension to have been built with zlib support

For using **redis** as caching method, PHP must have the [redis](https://pecl.php.net/package/redis) extension installed. The [redis\_compressed](https://stefangabos.github.io/Zebra_Database/Zebra_Database/Zebra_Database.html#var$redis_compressed) property, which hands the compressing over to the extension instead of doing it in the library, additionally requires that extension to have been built with `--enable-redis-lzf`

> Query results are compressed either way - the two *\_compressed* properties only decide whether the library or the caching server does it, and caching works without either of them.

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

[](#installation)

You can install Zebra Database via [Composer](https://packagist.org/packages/stefangabos/zebra_database)

```
# get the latest stable release
composer require stefangabos/zebra_database

# get the latest commit
composer require stefangabos/zebra_database:dev-master
```

Or you can install it manually by downloading the latest version, unpacking it, and then including it in your project

```
require_once 'path/to/Zebra_Database.php';
```

How to use
----------

[](#how-to-use)

##### Connecting to a database

[](#connecting-to-a-database)

```
// instantiate the library
$db = new Zebra_Database();

// connect to a server and select a database
$db->connect('host', 'username', 'password', 'database');
```

##### Running queries

[](#running-queries)

```
// question marks will re replaced automatically with the escaped values from the array
// I ENCOURAGE YOU TO WRITE YOUR QUERIES IN A READABLE FORMAT, LIKE BELOW
$db->query('
    SELECT
    	column1
        , column2
        , column3
    FROM
    	tablename1
    	LEFT JOIN tablename2 ON tablename1.column1 = tablename2.column1
    WHERE
    	somecriteria = ?
        AND someothercriteria = ?
', array($somevalue, $someothervalue));

// any fetch method will work with the last result so
// there's no need to explicitly pass that around

// you could fetch all records to one associative array...
$records = $db->fetch_assoc_all();

// you could fetch all records to one associative array
// using the values in a specific column as keys
$records = $db->fetch_assoc_all('column1');

// or fetch records one by one, as associative arrays
while ($row = $db->fetch_assoc()) {
    // do stuff
}
```

##### An INSERT statement

[](#an-insert-statement)

```
// notice that you can use MySQL functions in values
$db->insert(
    'tablename',
    array(
        'column1'      => $value1,
        'column2'      => $value2,
        'date_updated' => 'NOW()'
    )
);
```

##### An UPDATE statement

[](#an-update-statement)

```
// $criteria will be escaped and enclosed in grave accents, and will
// replace the corresponding ? (question mark) automatically
// also, notice that you can use MySQL functions in values
// when using MySQL functions, the value will be used as it is without being escaped!
// while this is ok when using a function without any arguments like NOW(), this may
// pose a security concern if the argument(s) come from user input.
// in this case we have to escape the value ourselves
$db->update(
    'tablename',
    array(
        'column1'      => $value1,
        'column2'      => 'TRIM(UCASE("value2"))',
        'column3'      => 'TRIM(UCASE("'' . $db->escape($value3) . "))',
        'date_updated' => 'NOW()'
    ),
    'criteria = ?',
    array($criteria)
);
```

> There are over **40 methods** and 20 properties that you can use and **lots** of things you can do with this library. I've prepared an [awesome documentation](https://stefangabos.github.io/Zebra_Database/Zebra_Database/Zebra_Database.html) so that you can easily get an overview of what can be done. Go ahead, [check it out](https://stefangabos.github.io/Zebra_Database/Zebra_Database/Zebra_Database.html)!

Running the tests
-----------------

[](#running-the-tests)

The test suite is not part of the released package - if you want to run them you need a git checkout of the repository.

The tests run against a real MySQL instance and **drop and recreate their tables at the start of every run** - `test_users`, `test_products` and `test_categories`, in a database of their own - so point them at a server you are happy for them to write to.

Running them wants **MySQL 5.7 or newer** - a requirement of the suite, not of the library, which still supports 4.1.22+. What sets that floor is strict mode: the suite asserts what MySQL does with a value too long for its column, and that is one thing under `STRICT_TRANS_TABLES` and quite another without it, so rather than have tests that pass either way the requirement is checked before the first test runs and reported if the server does not meet it. 5.7 is simply where strict mode became the default - an older server will do if it is switched on there, and the tables themselves only need 5.5.3 for `utf8mb4` - but 5.7 and up is what this is run against.

```
composer install

# copy the defaults and put your connection details in the copy (it is git-ignored)
cp tests/phpunit.xml.dist tests/phpunit.xml

tests/run-tests.sh
```

On Windows use `tests\run-tests.bat`, which does the same things in the same order.

Run it with no arguments and it checks everything - the suite, then the PHP compatibility, static analysis and coding standard checks. Give it any argument and it runs only the tests you asked for, which is what you want while working on something:

```
tests/run-tests.sh --testdox                    # readable output, tests only
tests/run-tests.sh --filter dcount              # only tests matching "dcount"
tests/run-tests.sh CacheTest.php                # a single file
tests/run-tests.sh --group regression           # every test guarding a past fix
tests/run-tests.sh --filter dcount --static     # the checks as well
PHP=/path/to/php7.4/bin/php tests/run-tests.sh  # a specific interpreter
```

The suite needs PHP 7.4 or newer. The library goes back further than that, and `tests/run-legacy.sh` is what backs that claim up - it lints everything that ships and round-trips a row on a real PHP 5.6.40 in a container, against a MySQL of its own, so it needs Docker running:

```
tests/run-legacy.sh         # lint, then the smoke test
tests/run-legacy.sh --lint  # the lint alone, which needs no database
```

The three checks are also available on their own, and those are the ones to reach for while working through whatever they report, since they take arguments of their own:

```
composer analyse               # phpstan
composer check-style           # the coding standard
composer check-compat          # PHP compatibility, against the supported version
composer check-compat-legacy   # ...and against the oldest version the library still runs on
```

A handful of tests cover the memcache and redis caching backends and skip themselves unless both the extension and a running server are found. Neither needs any configuration:

```
redis-server --port 6379 --save '' --appendonly no &
memcached -p 11211 -m 64 &
```

Two of those go further and skip unless the redis extension was built with `--enable-redis-lzf`, since what they check is that compression is handed over to the extension rather than done twice. All of them run on every push through [GitHub Actions](https://github.com/stefangabos/Zebra_Database/actions/workflows/tests.yml), against PHP 7.4 through 8.5 with both servers alongside.

Coverage is not produced on an ordinary run. Asking for it requires either [pcov](https://github.com/krakjoe/pcov) or [Xdebug](https://xdebug.org/) to be installed - without one of them PHPUnit reports *"No code coverage driver available"*, runs the tests anyway and writes no report:

```
composer test-coverage      # writes tests/coverage-html
```

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance81

Actively maintained with recent releases

Popularity42

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 96.3% 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 ~143 days

Recently: every ~84 days

Total

34

Last Release

19d ago

Major Versions

2.13.0 → 3.0.x-dev2025-08-31

2.13.2 → 3.0.02026-07-29

PHP version history (3 changes)2.8.2PHP &gt;=5.0.0

2.9.3PHP &gt;=5.2.0

3.0.0PHP &gt;=7.3.0

### Community

Maintainers

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

---

Top Contributors

[![stefangabos](https://avatars.githubusercontent.com/u/741165?v=4)](https://github.com/stefangabos "stefangabos (554 commits)")[![josephspurrier](https://avatars.githubusercontent.com/u/2394539?v=4)](https://github.com/josephspurrier "josephspurrier (6 commits)")[![primeinc](https://avatars.githubusercontent.com/u/4395149?v=4)](https://github.com/primeinc "primeinc (5 commits)")[![rayzru](https://avatars.githubusercontent.com/u/673428?v=4)](https://github.com/rayzru "rayzru (4 commits)")[![JeffBuckles](https://avatars.githubusercontent.com/u/10737611?v=4)](https://github.com/JeffBuckles "JeffBuckles (2 commits)")[![bmorg](https://avatars.githubusercontent.com/u/842699?v=4)](https://github.com/bmorg "bmorg (2 commits)")[![ebroda](https://avatars.githubusercontent.com/u/5451330?v=4)](https://github.com/ebroda "ebroda (2 commits)")

---

Tags

database-wrappermysqliphpphp-databasephp-wrapperprepared-statementsphpdatabasedebuggingmysqlcachingwrappersecuremysqlideveloper-toolsphp-databasesql-injection-preventiondatabase-securitymysql-wrapper

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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