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

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

php-comp/lite-db
================

a simple extended pdo tool library of the php

v1.1.0(7y ago)16012MITPHPPHP &gt;7.0.0

Since May 8Pushed 4y ago1 watchersCompare

[ Source](https://github.com/phppkg/pdox)[ Packagist](https://packagist.org/packages/php-comp/lite-db)[ Docs](https://github.com/php-comp/lite-database)[ RSS](/packages/php-comp-lite-db/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (3)Used By (2)

simple db client
================

[](#simple-db-client)

[![License](https://camo.githubusercontent.com/968bab789e2e61a4960591045a4e1e33d9ae96d7479b2be29cea806ca3b659fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f706870706b672f70646f782e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Php Version](https://camo.githubusercontent.com/5127919eba34cfd4efa2e1d76a62f25de4061ec0a31a434fd9b23f6b16fc2c0e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e302d627269676874677265656e2e7376673f6d61784167653d32353932303030)](https://packagist.org/packages/phppkg/pdox)[![Latest Stable Version](https://camo.githubusercontent.com/ebe11771e9a244e6243de834f2c2e96b7fb7feccbeee4dab71b1799f2994cdae/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706870706b672f70646f782e737667)](https://packagist.org/packages/phppkg/pdox)

Simple database client for mysql,sqlite by PDO

- **github**

Install
-------

[](#install)

- By composer require

```
composer require phppkg/pdox
```

- By composer.json

```
{
  "require": {
    "phppkg/pdox": "~1.0.0"
  }
}
```

- Pull directly

```
git clone https://github.com/phppkg/pdox.git
```

Usage
-----

[](#usage)

### create connection

[](#create-connection)

```
use PhpComp\PdoX\PdoX;

$db = PdoX::create([
    // open debug, will record query logs.
    'debug' => 1,

    'driver' => 'mysql', // 'sqlite' 'pgsql' 'mssql'
    'host' => 'localhost',
    'user' => 'root',
    'password' => 'password',
    'database' => 'test',
]);

// add event listeners.

$db->on(PdoX::CONNECT, function ($db) {
    echo "connect database success\n";
});
$db->on(PdoX::BEFORE_EXECUTE, function ($sql) {
    echo "Will run SQL: $sql\n";
});
$db->on(PdoX::DISCONNECT, function ($db) {
    echo "disconnect database success\n";
});
```

### basic

[](#basic)

```
$db->exec('CREATE TABLE IF NOT EXISTS `user` (
   `id` INT(11) NOT NULL AUTO_INCREMENT,
   `username` VARCHAR(32) NOT NULL,
   `nickname` VARCHAR(32),
   primary key(id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;');

// fetch all
$ret = $db->fetchAll('show tables');
var_dump($ret);
```

### insert

[](#insert)

```
// insert one
$ret = $db->insert('user', [
    'username' => 'tom',
    'nickname' => 'tom-nick',
], [
    'returnSql' => 1,
]);
var_dump($ret);

// batch insert
$ret = $db->insertBatch('user',[
    [
        'username' => 'tom',
        'nickname' => 'tom-nick',
    ],
    [
        'username' => 'tom1',
        'nickname' => 'tom-nick2',
    ],
], [
    'returnSql' => 1,
]);
var_dump($ret);
```

### query

[](#query)

```
// find one
// SQL: SELECT * FROM `user` WHERE `id`= ? LIMIT 1
$ret = $db->queryOne('user', ['id' => 3], '*', [
    'fetchType' => 'assoc',
    'returnSql' => 1,
]);
var_dump($ret);

// find all
// SQL: SELECT * FROM `user` WHERE `username` like ? LIMIT 1000
$ret = $db->queryAll('user', [ ['username', 'like', '%tes%'] ], '*', [
    'fetchType' => 'assoc',
    'limit' => 10,
    'returnSql' => 1,
]);
var_dump($ret);

// more conditions
$ret = $db->queryAll('user', [
     'userId' => 23,      // 'AND `userId` = 23'
     'title' => 'test',  // value will auto add quote, equal to "AND title = 'test'"
     'status' => [1, 2], // status IN (1,2)

     ['publishAt', '>', '0'],  // ==> 'AND `publishAt` > 0'
     ['createdAt', ' 6';
     }
]);

// SQL: SELECT * FROM `user` WHERE "name"= ? OR ( "type"= ? AND "createAt" queryAll('user', [
  'name' => 'tom',
  'or' => '(',
  'type' => 'admin',
  ['createAt', '
