PHPackages                             aslamhus/db - 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. aslamhus/db

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

aslamhus/db
===========

A class for handling mysql database

v3.0.2(1y ago)039MITPHP

Since Feb 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/aslamhus/DB)[ Packagist](https://packagist.org/packages/aslamhus/db)[ RSS](/packages/aslamhus-db/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (10)Used By (0)

DB
==

[](#db)

A Database class for mysqli prepared statements. Connect to your database and easily prepare statements, bind variables and execute queries. Currently Has methods for MYSQL select, insert, update and delete.

Install
-------

[](#install)

```
composer require aslamhus/db
```

Dependencies
------------

[](#dependencies)

1. Composer
2. composer package "vlucas/phpdotenv"
3. MYSQL database with access privileges
4. PHP

---

Getting started
---------------

[](#getting-started)

### Configuring your database

[](#configuring-your-database)

**DB** configures your database settings using environment variables. To set those up you'll need to install `vlucas/phpdotenv` and create a .env file with your database name, host, username and password.

#### 1. Install

[](#1-install)

If you have cloned the repo, install all dependencies.

```
composer install
```

If you are installing `@aslamhus/db` as a package, simply run

```
composer require @aslamhus/db
```

You should now have `vlucas/phpdotenv` in your vendor directory. More information on [vlucas/dotenv](https://github.com/vlucas/phpdotenv)

#### 2. Create .env file in your project root directory with the following variables

[](#2-create-env-file-in-your-project-root-directory-with-the-following-variables)

```
// sets the development mode (possible values, "DEV" or "PRODUCTION")
DEV = "DEV"
// your db hostname
DB_HOST = "localhost"
// your db name
DB_NAME = "db_name"
// your db user name
DB_USER = "db_user"
// your db password
DB_PASS = "db_password"
// your local db password
DB_LUSER = "local_db_user"
// your local db username
DB_LNAME = "local_db_password"
```

#### 3. Load your environment variables

[](#3-load-your-environment-variables)

create a config.php file with the following:

**Make sure you specify your path to vendor/autload and your .env file**

```
// require composer autoload
require_once '../vendor/autoload.php';
// path to your .env file
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__), '.env');
// load the variables
$dotenv->load() ;
// ensure the correct env variables are loaded
$dotenv->required(['DEV', 'DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS', 'DB_LUSER', 'DB_LNAME'])->notEmpty();
$dotenv->required('DEV')->allowedValues(['DEV', 'PRODUCTION']);
```

#### 4. Connect to the database

[](#4-connect-to-the-database)

Require your config.php file and then instantiate the DB class.

```
require_once 'config.php'

use Database\DB;

$db = new DB();
```

That's it! You should now be connected to your database. You can begin making queries.

### Optional: Allowlist for table names and columns

[](#optional-allowlist-for-table-names-and-columns)

If you would like to add an extra layer of security, you can create an allow list of valid table names and valid column names in your mysql queries.

Create two text files in the src directory, '.validcolumns' and '.validtables' Separate names of valid tables and valid columns by line breaks. `DB` will then only perform queries on valid columns or tables. If the files are left blank or do not exist, all tables and all columns will be allowed.

Testing
-------

[](#testing)

To see `DB` in action you can run `test/index.php` in a local environment with mysql and php installed.

Before running these examples you'll need to set up a test database.

1. Login to your local mysql with admin privileges
2. create a test database/test table by executing test/db/schema.sql
3. add values to your test database by executing seeds.sql

Now you can run test/index.php in your local environment.

Examples
--------

[](#examples)

### `select`

[](#select)

`select` takes two parameters with an optional third. The first parameter is the columns to select, the second is the tablename and the third is an optional array of conditions.

#### Select everything

[](#select-everything)

```
$result = $db->select('*', 'tablename');
print_r($result);
```

#### Select with conditions

[](#select-with-conditions)

In this example we select everything from the table 'users' where a the username has a value of the variable $username.

```
$username = 'bob sacamano';
$result = $db->select('*','users',
    [
        'username = ?' => $username
    ]
);
print_r($result);
```

... more examples to come!

Author
------

[](#author)

aslamhus

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance48

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community7

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

Recently: every ~244 days

Total

9

Last Release

388d ago

Major Versions

v1.0.0 → v2.0.12022-02-17

v2.3.0 → v3.0.02023-10-30

### Community

Maintainers

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

---

Top Contributors

[![aslamhus](https://avatars.githubusercontent.com/u/76789892?v=4)](https://github.com/aslamhus "aslamhus (21 commits)")

### Embed Badge

![Health badge](/badges/aslamhus-db/health.svg)

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

###  Alternatives

[mukadi/wordpress-bundle

Integrate wordpress and symfony in the same application

212.8k1](/packages/mukadi-wordpress-bundle)

PHPackages © 2026

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