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

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

tschoffelen/db.php
==================

Simple PHP class for doing standard MySQL actions.

1.1.1(7y ago)89353MIT

Since Jan 4Compare

[ Source](https://github.com/tschoffelen/db.php)[ Packagist](https://packagist.org/packages/tschoffelen/db.php)[ RSS](/packages/tschoffelen-dbphp/feed)WikiDiscussions Synced yesterday

READMEChangelog (3)DependenciesVersions (4)Used By (0)

Database.php
============

[](#databasephp)

[![Build Status](https://camo.githubusercontent.com/2c0e663959eb5b610c64f41297daf58bba15ff7783102d4a06234c47e83e995a/68747470733a2f2f7472617669732d63692e636f6d2f747363686f6666656c656e2f64622e7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/tschoffelen/db.php)[![codecov](https://camo.githubusercontent.com/917361428caf39c38fb6fa744c3bf8c5e6076c5d0339fd413662eb0ceb0d5cf8/68747470733a2f2f636f6465636f762e696f2f67682f747363686f6666656c656e2f64622e7068702f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/tschoffelen/db.php)

Database.php is a simple PHP class for doing standard MySQL actions, such as selecting, inserting, updating and deleting database rows. It also includes some nice functionality, like auto-escaping to protect your database from malicious code and automatic serializing of arrays.

Usage
-----

[](#usage)

### Initiating

[](#initiating)

**Initiate a database connection using by creating a `new Database()` object.**

```
require_once('Database.php');

$db = new Database($database_name, $username, $password, $host); // $host is optional and defaults to 'localhost'
```

### Select

[](#select)

**Select rows from a database table**

Usage:

```
$db->select($table, $where, $limit, $order, $where_mode, $select_fields)
```

Arguments:

- string `$table` - name of the table to select from
- array/string `$where` - array or string holding the filters/'WHERE' clause for the query
- int/string `$limit` - integer or string holding the 'LIMIT' clause
- string `$order` - string holding the 'ORDER BY' clause
- string `$where_mode` - whether to add an 'AND' or 'OR' after each item in the `$where` array, defaults to `AND`
- string `$select_fields` - the fields to select (SELECT &lt;$select\_fields&gt; FROM ...), defaults to `*`

Example:

```
// get the first 10 candy bars that are sweet, and order them by amount
$db->select('candy', ['sweet' => 1, 'spicy' => 0], 10, 'amount DESC');
```

```
// get the ids 1, 2,5,9  from products
$db->select('products', ['id' => 'in (1,2,5,9)'], false, false,'OR');
```

#### Reading results

[](#reading-results)

Reading the results can be done with the following functions:

- `$db->count()` returns the number of selected rows, equal to `mysql_num_rows()`
- `$db->result()` returns all matches rows as an array containing row objects
- `$db->row()` returns the first row that matches the query as an object
- `$db->result_array()` returns all matches rows as an array containing row arrays
- `$db->row_array()` returns the first row that matches the query as an array

Please note that you can call any of these functions also directly after the `$db->select()` call, like shown below:

```
echo $db->select('candy', ['sweet' => 1], 10)->count();
```

There are a few other methods available for queries that might come in handy:

- `$db->sql()` returns the sql query that was last executed

### Insert

[](#insert)

**Insert data into a database table**

Usage:

```
$db->insert($table, $fields=[])
```

Example:

```
$db->insert(
	'candy', [
		'name' => 'Kitkat original',
		'sweet' => 1,
		'spicey' => 0,
		'brand' => 'Kitkat',
		'amount_per_pack' => 4
	]
);
```

**Tip!** You can call `$db->id()` immeadiately after a `$db->insert()` call to get the ID of the last inserted row.

### Update

[](#update)

**Update one or more rows of a database table**

Usage:

```
$db->update($table, $fields=[], $where=[])
```

Example:

```
// set amount per pack to 5 for all Kitkats
$db->update(
	'candy', [
		// fields to be updated
		'amount_per_pack' => 5
	], [
		// 'WHERE' clause
		'brand' => 'Kitkat'
	]
);
```

### Delete

[](#delete)

**Remove one or more rows from a database table**

Usage:

```
$db->delete($table, $where=[])
```

Example:

```
// delete all Kitkat candy
$db->delete(
	'candy', [
		// 'WHERE' clause
		'brand' => 'Kitkat'
	]
);
```

### Singleton

[](#singleton)

**Access the database instance outside the global scope after initializing it**

Usage:

```
$my_db = Database::instance();
```

Example:

```
// Global scope
$db = new Database($database_name, $username, $password, $host);

// Function scope
function something() {
    // We could simply use `global $db;`, but using globals is bad. Instead we can do this:
    $db = Database::instance();

    // And now we have access to $db inside the function
}
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 81.8% 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 ~325 days

Total

3

Last Release

2814d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1af8c1e173ab229818dff16253e9b49ce9b0ec8847311d95390760eb59f9901e?d=identicon)[tschoffelen](/maintainers/tschoffelen)

---

Top Contributors

[![tschoffelen](https://avatars.githubusercontent.com/u/666220?v=4)](https://github.com/tschoffelen "tschoffelen (27 commits)")[![craiem](https://avatars.githubusercontent.com/u/3052948?v=4)](https://github.com/craiem "craiem (2 commits)")[![PierreG89](https://avatars.githubusercontent.com/u/5750914?v=4)](https://github.com/PierreG89 "PierreG89 (2 commits)")[![CodeLingoBot](https://avatars.githubusercontent.com/u/45469328?v=4)](https://github.com/CodeLingoBot "CodeLingoBot (1 commits)")[![nydacatapang](https://avatars.githubusercontent.com/u/20010803?v=4)](https://github.com/nydacatapang "nydacatapang (1 commits)")

### Embed Badge

![Health badge](/badges/tschoffelen-dbphp/health.svg)

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

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[pgvector/pgvector

pgvector support for PHP

198628.3k10](/packages/pgvector-pgvector)

PHPackages © 2026

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