PHPackages                             jaypha/mysqli-ext - 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. jaypha/mysqli-ext

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

jaypha/mysqli-ext
=================

Convenience extensions to mysqli

v0.1.8(6y ago)01671BSL-1.0PHPPHP ^5.0 || ^7.0

Since May 24Pushed 6y agoCompare

[ Source](https://github.com/jaypha/mysqli-ext)[ Packagist](https://packagist.org/packages/jaypha/mysqli-ext)[ RSS](/packages/jaypha-mysqli-ext/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (12)Used By (1)

Jaypha MySQL Ext
================

[](#jaypha-mysql-ext)

Written by Jason den Dulk

A trait containing convenience functions to extend the functionality of the `mysqli` class. These functions reflect very common database related tasks, and can help reduce code overhead.

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

[](#requirements)

PHP v5 or greater.

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

[](#installation)

```
composer require jaypha/mysqli-ext

```

MySqliExt
---------

[](#mysqliext)

### As Trait

[](#as-trait)

Must be added to a child of `mysqli`.

```
use MySQLiExtTrait;

```

### As Class

[](#as-class)

```
$conn = new Jaypha\MySQLiExt($host, $user, $password = NULL, $database = NULL)

```

API
---

[](#api)

### `q($query)`

[](#qquery)

Same as mysqli::query, but throws an exception upon error.

### `mq($query)`

[](#mqquery)

Same as mysqli::multi\_query, but throws an exception upon error.

### `queryValue($query)`

[](#queryvaluequery)

Calls `q` with `$query` and returns a single value. The first value of the first row.

Returns a string if a value is found and not null, NULL if the value was null, or false if no row was found

```
$n = $db->queryValue("select name from sometable where id='2'");
echo "name is $n";

```

### `queryRow($query, $resultType = MYSQLI_ASSOC)`

[](#queryrowquery-resulttype--mysqli_assoc)

Calls `q` with `$query` and returns the first row.

Returns an array if a row was found. If result type is MYSQLI\_ASSOC, it will be an associative array. Returns false if no row was found

```
$row = $db->queryRow("select * from sometable where id='2'");
print_r($row);

```

### `queryData($query, $keyField = NULL, $resultType = MYSQLI_ASSOC)`

[](#querydataquery-keyfield--null-resulttype--mysqli_assoc)

Calls `q` with `$query` and returns the whole data set. If `$keyField` is set, then the values from this column are used as array keys.

Returns an array of rows. Each row is an array. If resultType is MYSQLI\_ASSOC, it will be an associative array.

If now rows were found, returns an empty array.

```
$data = $db->queryData("select * from sometable");
foreach ($data as $row) { ... }

```

If a key is repeated, it will overwrite any existing value.

### `queryChunkedData($query, $limit = 1000, $resultType = MYSQLI_ASSOC)`

[](#querychunkeddataquery-limit--1000-resulttype--mysqli_assoc)

Returns a `MySQLiChunkedResult` instance.

### `queryColumn($query)`

[](#querycolumnquery)

Calls `q` with `$query` and returns a single column. If the SQL query selects one field, then `queryColumn` returns an array containing the values. If the query selects two or more, then an associative array is returned with the contents of the first column as the keys and the contents of the second columns as the values.

If no rows were found, returns an empty array.

If a key is repeated, it will overwrite any existing value.

```
$column = $db->queryColumn("select name from sometable");
print_r($column);

$assoc = $db->queryColumn("select id,name from sometable");
print_r($assoc);

```

### `insert($tableName, $columns, $values = NULL)`

[](#inserttablename-columns-values--null)

A shortcut for insert statements. There are three cases

- If `$values` is NULL, then `$columns` is assumed to be an associative array and it is inserted to the database using key/value pairs.
- If `$columns` is an array and `$values` is an array, then a single row is inserted using `$columns` and `$values`.
- If `$columns` is an array and `$values` is an array of arrays, then multiple rows are inserted. Each element of the `$values` array is considered a row.

Returns the insert ID value.

```
// insert into sometable set id=1, name='john'
$id = $db->insert("sometable", [ "id" => 1, "name" => "john" ]);
echo "new row ID is $id";

// insert into sometable (id,name) values (1,'john')"
$db->insert("sometable", [ "id", "name"], [1, "john"]);

// insert into sometable (id,name) values (1,'john'), (2,'jane')
$db->insert("sometable", [ "id", "name"], [[1, "john"], [2, "jane"]]);

```

### `update($tableName, $values, $wheres)`

[](#updatetablename-values-wheres)

A shortcut for update statements.

```
// update into sometable set name='john' where id=1
$db->update("sometable", [ "name" => "john" ], [ "id" => 1 ]);

```

### `replace($tableName, $values)`

[](#replacetablename-values)

A shortcut for replace statements.

```
// replace sometable set name='john', id=1
$db->replace("sometable", [ "name" => "john", "id" => 1 ]);

```

### `insertUpdate($tableName, $values, $wheres)`

[](#insertupdatetablename-values-wheres)

Will either insert or update. If a row mathcing the given `$wheres` exists, then it will be updated with the new `$values`. Otherwise a new row is inserted with the `$values` and `$wheres` combined.

Use this instead of replace when you do not want to destory existing rows.

```
// update into sometable set name='john' where id=1 (if exists)
// insert into sometable set name='john', id=1 (if not exists)
$db->insertUpdate("sometable", [ "name" => "john" ], [ "id" => 1 ]);

```

### `delete($tableName, $wheres = null)`

[](#deletetablename-wheres--null)

Shortcut for delete. If `$where` is an integer, then it is matched to an `id`column.

If `$wheres` is null, then it will truncate the table.

```
// delete from sometable where name='john'
$db->delete("sometable", [ "name" => "john" ]);

// delete from sometable where id=1
$db->delete("sometable", 1);

```

### `get($tableName, int $id)`

[](#gettablename-int-id)

Selects a row where column 'id' is of value `$id`. A column called 'id' must exist.

```
$row = $db->get("sometable", id);
assert(is_array($row));

```

### `set($tableName, $values, int $id = 0)`

[](#settablename-values-int-id--0)

Updates a table row where column 'id' is of value `id`. If no id is provided (i.e. a value of zero), performs an insert instead. A column called 'id' must exist.

Returns the row's id.

```
$db->set("sometable", [ "name" => "john" ], 1);
$id = $db->set("sometable", [ "name" => "jane" ]);

```

MySQLiChunkedResult
-------------------

[](#mysqlichunkedresult)

If, for whetever reason, you cannot have an open query, but you also cannot load the whole query result into memory, then this class works as a compromise. The result is stored into a temporary table, and then read in chunks as separate queries. Implements `Iterator` so it can be used with `foreach`.

### Usage

[](#usage)

```
$mysql = new MySQLiExt(...);
$result = new MySQLiChunkedResult($mysql, $query, $limit);
foreach ($result as $row) { ... }

$mysql = new MySQLiExt(...);
$result = $mysql->queryChunkedData($query, $limit);
foreach ($result as $row) { ... }

```

License
-------

[](#license)

Copyright (C) 2017 Jaypha.
Distributed under the Boost Software License, Version 1.0.
See [http://www.boost.org/LICENSE\_1\_0.txt](http://www.boost.org/LICENSE_1_0.txt)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

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

Every ~93 days

Recently: every ~154 days

Total

11

Last Release

2349d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9ca39c239f426fd20c6348188dc7cf2b2ff3783ebb5d44bd886572a8ec53b24f?d=identicon)[jaypha](/maintainers/jaypha)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jaypha-mysqli-ext/health.svg)

```
[![Health](https://phpackages.com/badges/jaypha-mysqli-ext/health.svg)](https://phpackages.com/packages/jaypha-mysqli-ext)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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