PHPackages                             ocolin/easydb - 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. ocolin/easydb

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

ocolin/easydb
=============

Simple, lightweight PDO database handler with environment variable support

v3.1.0(1mo ago)1471MITPHPPHP &gt;=8.2

Since Jun 6Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/ocolin/EasyDB)[ Packagist](https://packagist.org/packages/ocolin/easydb)[ Docs](https://github.com/ocolin/EasyDB)[ RSS](/packages/ocolin-easydb/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (7)Versions (13)Used By (1)

EasyDB
======

[](#easydb)

About
-----

[](#about)

EasyDB is a simple lightweight PDO wrapper for creating a quick database handler using environment variables.

EasyDB uses a prefix system so that you can add a DB handler with a single string parameter. That prefix tells EasyDB which environment parameters to load. So your code will look very short, easy, and readable.

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

[](#requirements)

This plugin was designed for PHP 8.2 or above.

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

[](#installation)

```
composer require Ocolin\Easy-db

```

Usage
-----

[](#usage)

### Quick connection from environment variables

[](#quick-connection-from-environment-variables)

#### Explanation

[](#explanation)

The key feature to this tool is the prefix. You provide a prefix string which will be used to find the needed environment variables that start with that prefix name. This saves you from having to specify the parameters needed and also allow you to have multiple handlers distinguished by prefix name.

Here are the environment variables that get loaded. In this example, the prefix is "PREFIX":

- PREFIX\_DB\_HOST
- PREFIX\_DB\_NAME
- PREFIX\_DB\_USER
- PREFIX\_DB\_PASS
- PREFIX\_DB\_PORT

#### Code example:

[](#code-example)

```
// Setting up variables manually for visual purposes
$_ENV['PREFIX_DB_HOST'] = 'localhost';
$_ENV['PREFIX_DB_NAME'] = 'mydb';
$_ENV['PREFIX_DB_USER'] = 'admin';
$_ENV['PREFIX_DB_PASS'] = 'password1234';
$_ENV['PREFIX_DB_PORT'] = 3306 // This is the default so does not need to be set.

$prefix_db = Ocolin\EasyDB\DB::fromEnv( prefix: 'PREFIX' );
```

If you don't have a means of loading environment variables or want to use this as a stand alone, you can specify a file with your environment variables:

```
/*
 * Contents of .env file:
 * MYDATA_DB_HOST=localhost
 * MYDATA_DB_NAME=mydb
 * MYDATA_DB_USER=admin
 * MYDATA_DB_PASS=password1234
 */

$prefix_db = Ocolin\EasyDB\DB::fromEnv(
    prefix: 'MYDATA', files: __DIR__ . '/../.env'
);
```

### Direct connection

[](#direct-connection)

EasyDB also has a direct connection function for use without environment variables.

```
$pdo_handler = Ocolin\EasyDB\DB::connect(
    host: 'localhost',
    name: 'mydb',
    user: 'admin',
    pass: 'password1234',
    port: 3306 // This is default and does not need to be set.
);
```

NOTE: the host parameter defaults to 'localhost', so it can be skipped when working on localhost.

### Options

[](#options)

By default EasyDB sets the following PDO options:

- PDO::ATTR\_ERRMODE =&gt; PDO::ERRMODE\_EXCEPTION
- PDO::ATTR\_STRINGIFY\_FETCHES =&gt; FALSE
- PDO::ATTR\_DEFAULT\_FETCH\_MODE =&gt; PDO::FETCH\_ASSOC

The options argument takes an array of options. These can override the defaults or ad additional options. Please see the PDO manual for which options and their settings are valid.

Query
-----

[](#query)

An additional helper library is added to assist with building some of the more basic SQL queries.

### createColumns

[](#createcolumns)

This function takes an array of database column names and create a coma separated string that can be used in an SQL query to specify columns to return in a SELECT query.

```
$output = Ocolin\EasyDB\Query::createColumns(
    params: [ 'A' => '1', 'B' => '2', 'C' => '3']
);
// output: `A`, `B`, `C`
```

### createColumnValues

[](#createcolumnvalues)

This function takes an associative array and builds a coma separated variable names for an INSERT type os query.

```
$output = Ocolin\EasyDB\Query::createColumnValues(
    params: [ 'A' => '1', 'B' => '2', 'C' => '3']
);
// output: ":A, :B, :C"
```

### replaceInto

[](#replaceinto)

Using the previous two functions we can create an SQL REPLACE INTO query providing just an array.

```
$output = Ocolin\EasyDB\Query::replaceInto(
    table: 'mytable', params: [ 'A' => '1', 'B' => '2', 'C' => '3' ]
);
/*
    REPLACE INTO mytable
        ( `A`, `B`, `C` )
    VALUES
        ( :A, :B, :C )
 */
```

### bindParameters

[](#bindparameters)

This function allows you to bind data to your PDO query parameters. It requires a pointer to your PDO statement handler, and an array of your data.

```
Ocolin\EasyDB\Query::bindParameters(
    query: $pdo_statement, params: [ 'A' => '1', 'B' => '2', 'C' => '3' ]
);
```

Used in conjunction with a function like replaceInto(), it will bind those array values to the matching parameter names in your PDO query statement.

### bindValues

[](#bindvalues)

This is similar to bind parameters but does so with values.

```
Ocolin\EasyDB\Query::bindValues(
    query: $pdo_statement, params: [ 'A' => '1', 'B' => '2', 'C' => '3' ]
);
```

### filterColumns

[](#filtercolumns)

This function is helpful when you have more data than you want to update. Perhaps you have an array with data that you don't want to overwrite such as the column index of the table. This basic function will strip out the columns that are not allowed.

```
$newparams = Ocolin\EasyDB\Query::filterColumns(
    params: [ 'A' => '1', 'B' => '2', 'C' => '3' ],
    allowed: [ 'A', 'B', 'D' ]
)

// $newparams = [ 'A' => '1', 'B' => '2' ]

$output = Ocolin\EasyDB\Query::replaceInto(
    table: 'mytable', params: $newparams
);
```

Validate
--------

[](#validate)

Another utility provided is a series of static functions for validating data being sent to the database. There is a function for each COLUMN data type and each function is named in format of "is" + the column type name.

The function will respond with TRUE if the data is valid, or an error string explaining why it is not valid. Here is a list of helper functions:

FunctionArgumentsArgument TypesisINTinput,unsignedmixed,boolisTINYINTinput,unsignedmixed,boolisSMALLINTinput,unsignedmixed,boolisMEDIUMINTinput,unsignedmixed,boolisBIGINTinput,unsignedmixed,boolisBOOLEANinputmixedisDECIMALinput,precision,scalemixed,int,intisFLOATinput,precision,scalemixed,int,intisCHARinput,lengthmixed,intisVARCHARinput,lengthmixed,intisTINYTEXTinputmixedisTEXTinputmixedisMEDIUMTEXTinputmixedisLONGTEXTinputmixedisENUMinput,allowedmixed,arrayisDATEinputmixedisTIMEinputmixedisTIMESTAMPinputmixedisDATETIMEinputmixedisYEARinputmixed### Example:

[](#example)

```
$output = Ocolin\EasyDB\Validate::isCHAR( input: ['A'] );
// "CHAR: Input must be a string, got array."

$output = Ocolin\EasyDB\Validate::isCHAR( input: 'A' );
// TRUE
```

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance90

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

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

Recently: every ~82 days

Total

12

Last Release

52d ago

Major Versions

1.6 → 2.02025-05-21

2.1 → v3.0.02026-03-23

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

databasemysqlpdoenvdb

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ocolin-easydb/health.svg)

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

###  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)[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)[aura/sqlschema

Provides facilities to read table names and table columns from a database using PDO.

41234.1k4](/packages/aura-sqlschema)

PHPackages © 2026

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