PHPackages                             ruckbeard/database - 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. ruckbeard/database

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

ruckbeard/database
==================

PHP MySQLi Database library

v0.2.3(11y ago)122MITPHPPHP &gt;=5.4

Since Mar 9Pushed 11y ago1 watchersCompare

[ Source](https://github.com/ruckbeard/php-database)[ Packagist](https://packagist.org/packages/ruckbeard/database)[ RSS](/packages/ruckbeard-database/feed)WikiDiscussions v0.2.x Synced 1mo ago

READMEChangelogDependenciesVersions (6)Used By (0)

php-database
============

[](#php-database)

A class for PHP that can be used to connect to a MySQL database, construct queries, and run queries. Similar to CodeIgniter's database class.

### **Query Functions**

[](#query-functions)

#### **query**

[](#query)

This will run a simple query on the database that will return a query object that can be used to obtain the results if it returns any rows.

```
$db = new Database;
$query = $db->query("SELECT * FROM table");
```

#### **get**

[](#get)

The get function accepts one optional parameter which is the table to put in the FROM portion of the query. When this is set, and no query constructor functions are called before it, it runs a basic query that selects all rows in the table. The get function can also be used run queries constructed by the query constructor functions as can be seen in the third example.

```
$db = new Database;
$query = $db->get("table");
```

*This will create a query "SELECT \* FROM table" and run it on the database*```
$db = new Database;

$db->select("*")->from("table")->where("id","5","=");
$query = $db->get();
```

*This will create a query "SELECT \* FROM table WHERE id = 5" and run it on the database*

### **The Query Result**

[](#the-query-result)

The query result object contains the results of the query that was run on the database. It is accessed as an object

#### **result**

[](#result)

The result function returns all of the results from the query. All of the results can be accessed by usings a foreach loop.

```
$db = new Database;
$query = $db->query("SELECT * FROM table");
foreach ($query->result() as $row) {
$row->id;
$row->name;
$row->date;
}
```

```
$db = new Database;

$query = $db->get("table");

foreach ($query->result() as $row) {
  $row->id;
  $row->name;
  $row->date;
}
```

#### **row**

[](#row)

The row function returns a single row from the query results. With no parameters, it will return the first row of the results. You can add a parameter to select which row you want to get.

; ```
$db = new Database;
$query = $db->query("SELECT * FROM table");
$row = $query->row();
$row->id;
$row->name;
$row->date;
```

or

```
$db = new Database;
$query = $db->query("SELECT * FROM table");
$row = $query->row(5);
$row->id;
$row->name;
$row->date;
```

### **Query Constructor Functions**

[](#query-constructor-functions)

The query constructor functions are a set of functions that can be called before using the get() function to write seperate portions of the query without having to manually write the query as a string. Information is automatically escaped by using this method. These functions can be chained together which can simplify the syntax and increase readability.

#### **select**

[](#select)

The select function writes the SELECT portion of the query.

```
$db = new Database;
$db->select("*");
$query = $db->get("table");
```

*This will create and run the query string "SELECT \* FROM table".*```
$db = new Database;

$db->select("foo,bar");
$query = $db->get("table");
```

*This will create and run the query string "SELECT foo,bar FROM table".*

```
$db = new Database;

$db->select("SELECT id");
$query = $db->get("table");
```

*This will create and run the query string "SELECT id FROM table".*

#### **select\_max**

[](#select_max)

The select max function writes the SELECT MAX(field) portion of the query. Accepts two parameters. The first is the field to select and the second renames the resulting field

```
$db = new Database;
$db->select_max("foo","bar");
$query = $db->get("table");
```

*This will create and run the query string "SELECT MAX(foo) as bar FROM table".*```
$db = new Database;

$db->select_max("foo");
$query = $db->get("table");
```

*This will create and run the query string "SELECT MAX(foo) as foo FROM table".*

#### **select\_min**

[](#select_min)

The select min function writes the SELECT MIN(field) portion of the query. Accepts two parameters. The first is the field to select and the second renames the resulting field

```
$db = new Database;
$db->select_min("foo","bar");
$query = $db->get("table");
```

*This will create and run the query string "SELECT MIN(foo) as bar FROM table".*```
$db = new Database;

$db->select_min("foo");
$query = $db->get("table");
```

*This will create and run the query string "SELECT MIN(foo) as foo FROM table".*

#### **select\_avg**

[](#select_avg)

The select avg function writes the SELECT AVG(field) portion of the query. Accepts two parameters. The first is the field to select and the second renames the resulting field

```
$db = new Database;
$db->select_avg("foo","bar");
$query = $db->get("table");
```

*This will create and run the query string "SELECT AVG(foo) as bar FROM table".*```
$db = new Database;

$db->select_avg("foo");
$query = $db->get("table");
```

*This will create and run the query string "SELECT AVG(foo) as foo FROM table".*

#### **select\_sum**

[](#select_sum)

The select sum function writes the SELECT SUM(field) portion of the query. Accepts two parameters. The first is the field to select and the second renames the resulting field

```
$db = new Database;
$db->select_sum("foo","bar");
$query = $db->get("table");
```

*This will create and run the query string "SELECT SUM(foo) as bar FROM table".*```
$db = new Database;

$db->select_sum("foo");
$query = $db->get("table");
```

*This will create and run the query string "SELECT SUM(foo) as foo FROM table".*

#### **from**

[](#from)

The from function will write the FROM portion of the query string.

```
$db = new Database;
$db->from("table");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table".*```
$db = new Database;

$db->select("foo");
$db->from("table");
$query = $db->get();
```

*This will create and run the query string "SELECT foo FROM table".*

#### **join**

[](#join)

The join function will write the JOIN portion of the query string. It accepts three parameters. The first parameter is the table to join. The second parameter contains the foreign key to join the queries by. The third parameter is optional. It sets what type of JOIN, such as left, right, or inner.

```
$db = new Database;
$db->select("table., table2.name");
$db->join("table2", "table.id = table2.id", "inner");
$query = $db->get("table");
```

**This will create and run the query string "SELECT table.*, table2.name FROM table INNER JOIN table2 ON table.id = table2.id".*#### **where**

[](#where)

The where function will write the WHERE portion of the query string. The function accepts three parameters. The first parameter can be written three different ways. It can be just the field, or the field and an operator. It can also be the field, operator, and data to find in the field. The second parameter is optional and should be set if the first parameter is set as just the field with optional operator. The second parameter is the data to search the field for. The third option can be set the operator if it hasn't been set in the first parameter. The default of the third parameter is "=". The first parameter can also be an array to chain with AND. The function can also be called multiple times to chain with AND.

```
$db = new Database;
$db->select("*");
$db->from("table");
$db->where("id = 1");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table WHERE id = '1'".*```
$db = new Database;

$db->select("*");
$db->from("table");
$db->where("id =", "1");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table WHERE id = '1'".*

```
$db = new Database;

$db->select("*");
$db->from("table");
$db->where("id", "1");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table WHERE id = '1'".*

```
$db = new Database;

$db->select("*");
$db->from("table");
$db->where("id","1","=");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table WHERE id = '1'".*

```
$db = new Database;

$db->select("*");
$db->from("table");
$db->where("id", "1");
$db->where("name", "bob");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table WHERE id = '1' AND name = 'bob'".*

#### **or\_where**

[](#or_where)

The where\_or function will write the WHERE portion of the query string. The function accepts three parameters. The first parameter can be written three different ways. It can be just the field, or the field and an operator. It can also be the field, operator, and data to find in the field. The second parameter is optional and should be set if the first parameter is set as just the field with optional operator. The second parameter is the data to search the field for. The third option can be set the operator if it hasn't been set in the first parameter. The default of the third parameter is "=". The first parameter can also be an array to chain with OR. The function can also be called multiple times to chain with OR.

```
$db = new Database;
$db->select("*");
$db->from("table");
$db->where("id", "1");
$db->where("name", "bob");
$db->where_or("name", "ted");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table WHERE id = '1' AND name = 'bob' OR name = 'ted'".*#### **group\_by**

[](#group_by)

The group\_by function will write the GROUP BY portion of the query string. The function accepts one parameter which can be a string of values to group by, or an array of multiple values.

```
$db = new Database;
$db->select("*");
$db->from("table");
$db->group_by("title, date");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table GROUP BY title, date"*```
$db = new Database;

$db->select("*");
$db->from("table");
$db->group_by(array("title","date"));
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table GROUP BY title, date"*

#### **distinct**

[](#distinct)

The distinct function will allow you to add DISTINCT to the SELECT portion of the query string.

```
$db = new Database;
$db->distinct();
$query = $db->get("table");
```

*This will create and run the query string "SELECT DISTINCT \* FROM table"*#### **having**

[](#having)

The having function will write the HAVING portion of the query string. The function accepts three parameters. The first parameter can be written three different ways. It can be just the field, or the field and an operator. It can also be the field, operator, and data to find in the field. The second parameter is optional and should be set if the first parameter is set as just the field with optional operator. The second parameter is the data to search the field for. The third option can be set the operator if it hasn't been set in the first parameter. The default of the third parameter is "=". The first parameter can also be an array to chain with AND. The function can also be called multiple times to chain with AND.

```
$db = new Database;
$db->select("*");
$db->from("table");
$db->having("id = 1");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table HAVING id = '1'".*```
$db = new Database;

$db->select("*");
$db->from("table");
$db->having("id =", "1");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table HAVING id = '1'".*

```
$db = new Database;

$db->select("*");
$db->from("table");
$db->having("id", "1");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table HAVING id = '1'".*

```
$db = new Database;

$db->select("*");
$db->from("table");
$db->having("id","1","=");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table HAVING id = '1'".*

```
$db = new Database;

$db->select("*");
$db->from("table");
$db->having("id", "1");
$db->having("name", "bob");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table HAVING id = '1' AND name = 'bob'".*

#### **or\_having**

[](#or_having)

The having\_or function will write the HAVING portion of the query string. The function accepts three parameters. The first parameter can be written three different ways. It can be just the field, or the field and an operator. It can also be the field, operator, and data to find in the field. The second parameter is optional and should be set if the first parameter is set as just the field with optional operator. The second parameter is the data to search the field for. The third option can be set the operator if it hasn't been set in the first parameter. The default of the third parameter is "=". The first parameter can also be an array to chain with OR. The function can also be called multiple times to chain with OR.

```
$db = new Database;
$db->select("*");
$db->from("table");
$db->having("id", "1");
$db->having("name", "bob");
$db->having_or("name", "ted");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table HAVING id = '1' AND name = 'bob' OR name = 'ted'".*#### **order\_by**

[](#order_by)

The order\_by function will allow you to write the ORDER BY portion of the query. This function accepts two parameters. The first is the field to order by. The second is the direction of the result, which are asc, desc, or random. The function can be called multiple times for multiple fields.

```
$db = new Database;
$db->order_by("id","desc");
$query = $db->get("table");
```

*This will create and run the query string "SELECT \* FROM table ORDER BY id DESC"*```
$db = new Database;

$db->order_by("id","desc");
$db->order_by("name","asc");
$query = $db->get("table");
```

*This will create and run the query string "SELECT \* FROM table ORDER BY id DESC, name ASC"*

#### **limit**

[](#limit)

The limit function will allow you to write the LIMIT portion of the query. This function accepts two parameters. The first is amount to limit the query by. The second is an optional offset.

```
$db = new Database;
$db->limit(10);
$query = $db->get("table");
```

*This will create and run the query string "SELECT \* FROM table LIMIT 10"*```
$db = new Database;

$db->limit(10,20);
$query = $db->get("table");
```

*This will create and run the query string "SELECT \* FROM table LIMIT 20, 10"*

#### **Chaining Constructor Functions**

[](#chaining-constructor-functions)

The query constructor functions can be chained together to simplify the syntax and increase readability.

```
$db = new Database;
$db->select("*")->from("table")->where("id","1","!=")->order_by("id","asc");
$query = $db->get();
```

*This will create and run the query string "SELECT \* FROM table WHERE id != 1 ORDER BY id ASC"*### **Insert**

[](#insert)

The insert function can be used to create and run insert queries. The first parameter it accepts in the table and the second is the data to insert into the query string. The data can be stored in an array or an object. The second parameter is optional. The data can be set by using the set() function.

```
$db = new Database;
$data = array(
field_1 => "foo",
field_2 => "bar",
field_3 => "foobar"
)
$db->insert("table",$data);
```

*This will create and run the query "INSERT INTO table (field\_1,field\_2,field\_3) VALUES ('foo','bar','foobar')*```
$db = new Database;

$data = array(
  array (
    field_1 => "foo1",
    field_2 => "bar2",
    field_3 => "foobar3"
  ),
  array (
    field_1 => "foo4",
    field_2 => "bar5",
    field_3 => "foobar6"
  )
)

$db->insert("table",$data);
```

*This will create and run the query "INSERT INTO table (field\_1,field\_2,field\_3) VALUES ('foo1','bar2','foobar3'),('foo4','bar5','foobar6')*

### **Update**

[](#update)

The update function will create and run an UPDATE query. The first paremeter is the table. The second parameter is the data to enter into the query string. The third parameter writes the WHERE portion of the query string. The second and third parameters are optional. The data can be set by using the set() function and the WHERE portion can be set by using the where() function.

```
$data = array(
  field_1 => "foo",
  field_2 => "bar",
  field_3 => "foobar"
)
$db->update("table",$data,"id = 1");
```

*This will create and run the query string "UPDATE table SET field\_1 = 'foo', field\_2 = 'bar', field\_3 = 'foobar' WHERE id = 1"*```
$data = array(
  field_1 => "foo",
  field_2 => "bar",
  field_3 => "foobar"
)

$db->where("id","1");
$db->update("table",$data);
```

*This will create and run the query string "UPDATE table SET field\_1 = 'foo', field\_2 = 'bar', field\_3 = 'foobar' WHERE id = 1"*

### **Set**

[](#set)

The set function can be used to set the data of an INSERT or UPDATE query string. It can be called multiple times to set multiple fields with data.

```
$db = new Database;
$db->set("field_1", "foo");
$db->set("field_2", "bar");
$db->set("field_3", "foobar");
$db->insert("table");
```

```
$db = new Database;

$db->set("field_1", "foo");
$db->set("field_2", "bar");
$db->set("field_3", "foobar");
$db->where("id","1");
$db->update("table");
```

### **Delete**

[](#delete)

The delete function can be called to create and run a DELETE query string. The first and second parameters are optional. The first parameter is the table to delete from and the second parameter writes the WHERE portion of the query string. These can also be set by using the from() and where() functions.

```
$db = new Database;
$db->from("table")->where("id","1");
$db->delete();
```

or

```
$db = new Database;

$db->delete("table", "id = 1");
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 69.4% 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 ~1 days

Total

5

Last Release

4086d ago

### Community

Maintainers

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

---

Top Contributors

[![ruckbeard](https://avatars.githubusercontent.com/u/7199017?v=4)](https://github.com/ruckbeard "ruckbeard (25 commits)")[![vulpes-rex](https://avatars.githubusercontent.com/u/96604121?v=4)](https://github.com/vulpes-rex "vulpes-rex (11 commits)")

---

Tags

databasemysqlmysqli

### Embed Badge

![Health badge](/badges/ruckbeard-database/health.svg)

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

###  Alternatives

[sergeytsalkov/meekrodb

The Simple PHP/MySQL Library

341387.0k10](/packages/sergeytsalkov-meekrodb)[ezsql/ezsql

Advance database access library. Make interacting with a database ridiculously easy. An universal interchangeable CRUD system.

86946.7k](/packages/ezsql-ezsql)[stefangabos/zebra_database

An advanced, compact and lightweight MySQL database wrapper library, built around PHP's MySQLi extension.

11812.0k](/packages/stefangabos-zebra-database)[go/db

Database library

6624.1k](/packages/go-db)[krugozor/database

PHP class library for simple, convenient, fast and safe work with MySql database, using PHP mysqli extension and imitation of prepared queries.

392.5k](/packages/krugozor-database)[dabble/dabble

Lightweight wrapper and helpers for MySQLi

1210.4k](/packages/dabble-dabble)

PHPackages © 2026

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