PHPackages                             impeck/safemysql - 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. impeck/safemysql

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

impeck/safemysql
================

PHP class designed for secure and efficient MySQL query handling

v1.2.0(2y ago)08Apache-2.0PHPPHP ^7.4 || ^8.0

Since Aug 24Pushed 2y agoCompare

[ Source](https://github.com/Impeck/safemysql)[ Packagist](https://packagist.org/packages/impeck/safemysql)[ Docs](https://github.com/colshrapnel/safemysql)[ RSS](/packages/impeck-safemysql/feed)WikiDiscussions master Synced 1mo ago

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

SafeMySQL
=========

[](#safemysql)

English | [Русский](https://github.com/Impeck/safemysql/blob/master/README.ru.md)

SafeMySQL is a PHP class designed for secure and efficient MySQL query handling.

Forked from [colshrapnel/safemysql](https://github.com/colshrapnel/safemysql).

It stands out for several key features:

- **Safety:** All dynamic query parts are incorporated into the query using placeholders, enhancing security.
- **Convenience:** It streamlines application code, reducing redundancy, and following the DRY (Don't Repeat Yourself) principle.

Features
--------

[](#features)

SafeMySQL offers three primary features that distinguish it from standard libraries:

1. **Type-Hinted Placeholders:** Unlike traditional libraries, SafeMySQL employs type-hinted placeholders for all query elements.
2. **Streamlined Usage:** It eliminates the need for repetitive binding and fetching, thanks to a range of helper methods.
3. **Partial Placeholder Parsing:** SafeMySQL allows placeholder parsing in any part of the query, making complex queries as easy as standard ones through the **parse()** method.

Getting Started
---------------

[](#getting-started)

Using SafeMySQL is straightforward. Here are the key steps:

1. Always use placeholders for dynamic data in your queries.
2. Mark each placeholder with a data type, including:
    - ?s ("string"): For strings (including `DATE`, `FLOAT`, and `DECIMAL`).
    - ?i ("integer"): For integers.
    - ?n ("name"): For identifiers (table and field names).
    - ?a ("array"): For complex placeholders used with the `IN()` operator (substituted with a string in 'a,'b,'c' format, without parentheses).
    - ?u ("update"): For complex placeholders used with the `SET` operator (substituted with a string in `field`='value',`field`='value' format).
    - ?p ("parsed"): A special placeholder type for inserting pre-parsed statements without further processing to avoid double parsing.
3. Utilize helper methods to retrieve data from queries, including:
    - `query($query, $param1, $param2, ...)`: Returns a mysqli resource.
    - `getOne($query, $param1, $param2, ...)`: Returns a scalar value.
    - `getRow($query, $param1, $param2, ...)`: Returns a 1-dimensional array (a row).
    - `getCol($query, $param1, $param2, ...)`: Returns a 1-dimensional array (a column).
    - `getAll($query, $param1, $param2, ...)`: Returns a 2-dimensional array (an array of rows).
    - `getInd($key, $query, $par1, $par2, ...)`: Returns an indexed 2-dimensional array (an array of rows).
    - `getIndCol($key, $query, $par1, $par2, ...)`: Returns a 1-dimensional array (an indexed column) consisting of key =&gt; value pairs.
4. For complex cases, rely on the **parse()** method.

### Example Usage

[](#example-usage)

Here are some examples of how to use SafeMySQL:

```
$name = $db->getOne('SELECT name FROM table WHERE id = ?i', $_GET['id']);
$data = $db->getInd('id', 'SELECT * FROM ?n WHERE id IN (?a)', 'table', [1, 2]);
$data = $db->getAll("SELECT * FROM ?n WHERE mod=?s LIMIT ?i", $table, $mod, $limit);
```

The standout feature of SafeMySQL is its type-hinted placeholders. This approach extends beyond simple scalar data, allowing you to include identifiers, arrays for the `IN` operator, and arrays for `INSERT` and `UPDATE` queries. No more struggling with binding field names or constructing complex queries manually.

For instance, consider binding a field name effortlessly:

```
$field = $_POST['field'];
$value = $_POST['value'];
$sql   = "SELECT * FROM table WHERE ?n LIKE ?s";
$data  = $db->query($sql, $field, "%$value%");
```

Simplifying queries involving arrays for the `IN` operator:

```
$array = [1, 2, 3];
$data  = $db->query("SELECT * FROM table WHERE id IN (?a)", $array);
```

The same convenience extends to complex queries like `INSERT` and `UPDATE`.

SafeMySQL also provides a set of helper functions, making database calls for everyday tasks quick and straightforward.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 52.6% 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

992d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/627a7b81f9691c16ecc0561f69981f77ee910d71c97c740f6b081c8417bccfab?d=identicon)[Impeck](/maintainers/Impeck)

---

Top Contributors

[![colshrapnel](https://avatars.githubusercontent.com/u/2895470?v=4)](https://github.com/colshrapnel "colshrapnel (40 commits)")[![Impeck](https://avatars.githubusercontent.com/u/1071117?v=4)](https://github.com/Impeck "Impeck (28 commits)")[![PeterMortensen](https://avatars.githubusercontent.com/u/2476480?v=4)](https://github.com/PeterMortensen "PeterMortensen (2 commits)")[![Habetdin](https://avatars.githubusercontent.com/u/15926758?v=4)](https://github.com/Habetdin "Habetdin (1 commits)")[![kbaryshnikov](https://avatars.githubusercontent.com/u/7947177?v=4)](https://github.com/kbaryshnikov "kbaryshnikov (1 commits)")[![00dev](https://avatars.githubusercontent.com/u/15100894?v=4)](https://github.com/00dev "00dev (1 commits)")[![rik43](https://avatars.githubusercontent.com/u/1737715?v=4)](https://github.com/rik43 "rik43 (1 commits)")[![DavidePastore](https://avatars.githubusercontent.com/u/1949364?v=4)](https://github.com/DavidePastore "DavidePastore (1 commits)")[![davidmz](https://avatars.githubusercontent.com/u/132120?v=4)](https://github.com/davidmz "davidmz (1 commits)")

---

Tags

mysqldb

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/impeck-safemysql/health.svg)

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

###  Alternatives

[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4572.9M34](/packages/aura-sqlquery)[envms/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

925511.7k13](/packages/envms-fluentpdo)[lichtner/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

921274.8k6](/packages/lichtner-fluentpdo)[fpdo/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

921244.9k7](/packages/fpdo-fluentpdo)[danielme85/laravel-log-to-db

Custom Laravel Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality.

135934.5k1](/packages/danielme85-laravel-log-to-db)[colshrapnel/safemysql

A real safe and convenient way to handle MySQL queries.

400103.5k4](/packages/colshrapnel-safemysql)

PHPackages © 2026

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