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

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

xuanyan/database
================

11462[2 issues](https://github.com/xuanyan/Database/issues)PHP

Since May 20Pushed 13y ago1 watchersCompare

[ Source](https://github.com/xuanyan/Database)[ Packagist](https://packagist.org/packages/xuanyan/database)[ RSS](/packages/xuanyan-database/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

the database abstraction supports mysql mysqli PDO SQLiteDatabase AND SQLite3 now, and will support more in future…
for more infomations plz [hit here](https://github.com/xuanyan/Database)

install with composer
---------------------

[](#install-with-composer)

```php5
{
 “require”: {
 “xuanyan/database”: “dev-master”
 }
}
```

how to use it:
--------------

[](#how-to-use-it)

### database initialization:

[](#database-initialization)

```php5
&lt;?php
require\_once ‘Database.php’;

// pdo
$db = Database::connect(‘pdo’, ‘mysql:dbname=test;host=localhost’, ‘root’, ‘root’);
$db = new Database(‘pdo’, ‘mysql:dbname=test;host=localhost’, ‘root’, ‘root’);

$db = Database::connect(array(‘pdo’, ‘mysql:dbname=test;host=localhost’, ‘root’, ‘root’));
$db = new Database(array(‘pdo’, ‘mysql:dbname=test;host=localhost’, ‘root’, ‘root’));

$link = new PDO;
$db = Database::connect($link);
$db = new Database($link);

// mysql
$db = Database::connect(‘mysql’, ‘localhost’, ‘root’, ‘root’, ‘test’);
$db = new Database(‘mysql’, ‘localhost’, ‘root’, ‘root’, ‘test’);

$db = Database::connect(array(‘mysql’, ‘localhost’, ‘root’, ‘root’, ‘test’));
$db = new Database(array(‘mysql’, ‘localhost’, ‘root’, ‘root’, ‘test’));

$link = mysql\_connect(‘localhost’, ‘root’, ‘root’);
mysql\_select\_db(‘test’, $link);
$db = Database::connect($link);
$db = new Database($link);

// mysqli
$db = Database::connect(‘mysqli’, ‘localhost’, ‘root’, ‘root’, ‘test’);
$db = new Database(‘mysqli’, ‘localhost’, ‘root’, ‘root’, ‘test’);

$db = Database::connect(array(‘mysqli’, ‘localhost’, ‘root’, ‘root’, ‘test’));
$db = new Database(array(‘mysqli’, ‘localhost’, ‘root’, ‘root’, ‘test’));

$link = new mysqli(‘localhost’, ‘root’, ‘root’, ‘test’);
$db = Database::connect($link);
$link = mysqli\_init();
$link→real\_connect(‘localhost’, ‘root’, ‘root’, ‘test’);
$db = Database::connect($link);
$db = new Database($link);

// sqlite
$db = Database::connect(‘sqlite’, ‘test.sqlite’);
$db = new Database(‘sqlite’, ‘test.sqlite’);

$db = Database::connect(array(‘sqlite’, ‘test.sqlite’));
$db = new Database(array(‘sqlite’, ‘test.sqlite’));

$link = new SQLiteDatabase(‘test.sqlite’);
$db = Database::connect($link);
$db = new Database($link);

$link = sqlite\_open(‘test.sqlite’);
$db = Database::connect($link);
$db = new Database($link);

// sqlite3
$db = Database::connect(‘sqlite3’, ‘test.sqlite3’);
$db = new Database(‘sqlite3’, ‘test.sqlite3’);

$db = Database::connect(array(‘sqlite3’, ‘test.sqlite3’));
$db = new Database(array(‘sqlite3’, ‘test.sqlite3’));

$link = new SQLite3(‘test.sqlite3’);
$db = Database::connect($link);
$db = new Database($link);

?&gt;
```

### use the $instance variable

[](#use-the-instance-variable)

```php5
&lt;?php
require\_once ‘Database.php’;

// set the Database::$instance
Database::$instance = Database::connect(‘mysql’, ‘localhost’, ‘root’, ‘root’, ‘test’);

//in some function, u can use them like this:

print\_r(test());

function test() {
 $sql = “SELECT \* FROM test WHERE id = ?”;
 $data = Database::$instance→getRow($sql, 1);

return $data; }
?&gt;
```

### demo code:

[](#demo-code)

```php5
&lt;?php
// origin sql
$sql = “SELECT \* FROM test\_table WHERE id = ‘$id’ AND name = ‘$name’”;
$result = $db→getAll($sql);

// if there are variables in sql, you can do it like this, and don’t need to process the variables. : )
$sql = “SELECT \* FROM test\_table WHERE id = ? AND name = ?”;
$result = $db→getAll($sql, $id, $name);

// you can allso use:
$sql = “SELECT \* FROM test\_table WHERE id = ? AND name = ?”;
$result = $db→getAll($sql, array($id, $name));

// it allso support:
$sql = “SELECT \* FROM test\_table WHERE id = :id AND name = :name”;
$result = $db→getAll($sql, array(‘name’=&gt;$name, ‘id’=&gt;$id));
?&gt;
```

### database methods:

[](#database-methods)

```php5
&lt;?php
public function getRow();
// get a row result
public function getCol();
// get a col result
public function getOne();
// get a column value
public function getAll();
// get all results
public function exec();
// execute a sql
public function lastInsertId();
// get the id of the last inserted row or sequence value
public function getDriver();
// get the origin link or object of the database driver
public function query();
// execute a sql and returns a statement object
public function fetch($query);
// fetch a result whith the statement object from query
?&gt;
```

### the class just do the connecting work at initialization. you can exec ‘the initialization sql’ after connect like this, sure, it is lazy-executing just like the connecting.

[](#the-class-just-do-the-connecting-work-at-initialization-you-can-exec-the-initialization-sql-after-connect-like-this-sure-it-is-lazy-executing-just-like-the-connecting)

```php5
&lt;?php
//mysql
$db = Database::connect(‘mysql’, ‘localhost’, ‘root’, ‘root’, ‘test’);
$db→initialization = array(
 ‘SET character\_set\_connection=utf8, character\_set\_results=utf8, character\_set\_client=binary’,
 “SET sql\_mode=’’”
);
// it’s same as above
$db→setConfig(‘initialization’, array(
 ‘SET character\_set\_connection=utf8, character\_set\_results=utf8, character\_set\_client=binary’,
 “SET sql\_mode=’’”
));
?&gt;
```

### use table-prefix

[](#use-table-prefix)

```php5
&lt;?php
//mysql
$db = Database::connect(‘mysql’, ‘localhost’, ‘root’, ‘root’, ‘test’);

$db→setConfig(‘tablePreFix’, ‘db\_’);
// or set multi table-prefix
$db→setConfig(‘tablePreFix’, array(
 ‘db1\_’=&gt;array(‘table1’, ‘table2’),
 ‘db2\_’=&gt; ‘\*’ // means the other tables use ‘db2\_’ prefix
));

// and u can get the table name by getTable function

$table\_name = $db→getTable(‘table1’); // db1\_table1

// and the sql will be auto replaced the table name with ‘tablePreFix’ by dafault if you use ‘{{tablename}}’ in sql

$sql = “SELECT \* FROM {{table1}}”;
// SELECT \* FROM db1\_table1
$result = $db→getAll($sql);

// and you can disable the auto-replace by using:

$db→setConfig(‘replaceTableName’, false);

?&gt;
```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5eb3f37500c8f11732193257a74160d77428ef03d260d0d84a5471f25fabf41f?d=identicon)[xuanyan](/maintainers/xuanyan)

---

Top Contributors

[![xuanyan](https://avatars.githubusercontent.com/u/25290?v=4)](https://github.com/xuanyan "xuanyan (24 commits)")

### Embed Badge

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

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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