PHP code example of proff56 / pdb
1. Go to this page and download the library: Download proff56/pdb library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
proff56 / pdb example snippets
use Mlevent\Pdb;
/**
* MYSQL
*/
$db = new Pdb([
'database' => 'ecommerce',
'username' => 'root',
'password' => 'test'
]);
/**
* SQLITE
*/
$db = new Pdb([
'driver' => 'sqlite',
'database' => 'ecommerce.sqlite'
]);
use Mlevent\Pdb;
$db = new Pdb([
'database' => 'ecommerce',
'username' => 'root',
'password' => 'test'
]);
[
'host' => 'localhost',
'driver' => 'mysql',
'database' => '',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'debug' => false,
'cacheTime' => 60,
'cachePath' => __DIR__ . '/Cache'
]
$products = $db->get('products');
foreach ($products as $product) {
echo $product->name;
}
$query = $db->select('id, name, code, price, stock')
->table('products')
->between('price', 900, 1500)
->grouped( function($q) {
$q->like(['code', 'name'], '%iphone%')
->orWhere('featured', 1);
})
->in('categoryId', [1, 2, 3, 4, 5, 6])
->order('price')
->get();
$products = $db->table('products')
->toArray()
->get();
foreach ($products as $product) {
echo $product['name'];
}
$products = $db->table('products')
->toJson()
->get();
$user = $db->table('users')
->first();
echo $user->email;
$email = $db->table('users')
->where('name', 'Walter')
->value('email');
echo $email;
$pluck = $db->table('products')
->pluck('name');
Array
(
[0] => Apple Iphone X 128 GB
[1] => Apple Iphone X 256 GB
[2] => Apple Iphone X 512 GB
)
$pluck = $db->table('products')
->pluck('name', 'code');
Array
(
[APPLEX128] => Apple Iphone X 128 GB
[APPLEX256] => Apple Iphone X 256 GB
[APPLEX512] => Apple Iphone X 512 GB
)
$user = $db->table('users')
->find(15);
echo $user->name;
$total = $db->table('users')
->where('userGroup', 'Admin')
->total();
echo $db->rowCount();
echo $db->lastInsertId();
$results = $db->raw('SELECT * FROM products WHERE active = ? AND MONTH(created) = MONTH(NOW())', 1)
->get();
$update = $db->raw('UPDATE payments SET active = !active WHERE status = ?', ['paid'])
->exec();
$posts = $db->table('posts')
->pager(25)
->get();
foreach ($posts as $post) {
echo $post->title;
}
echo $db->pagerLinks();
$db->pager(25, 'page');
echo $db->pagerLinks();
var_dump($db->pagerData());
Array
(
[count] => 255
[limit] => 10
[offset] => 0
[total] => 26
[current] => 1
)
$db->setPagerTemplate('<li>
<a class="{active}" href="{url}">
{text}
</a>
</li>');
$results = $db->cache(30)->get('comments');
$results = $db->redis(30)->get('comments');
$redisConnect = (function(){
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379, 1, NULL, 0, 0, ['auth' => ['default', '']]);
return $redis;
});
$db->setRedis($redisConnect());
$db->table('products')->insert([
'name' => 'Apple Iphone X 128 Gb',
'code' => 'APPLEX128',
'price' => '999.9'
]);
$db->table('products')->insert([
['name' => 'Apple Iphone X 128 Gb', 'code' => 'APPLEX128', 'price' => '999.9'],
['name' => 'Apple Iphone X 256 Gb', 'code' => 'APPLEX256', 'price' => '1149.9'],
['name' => 'Apple Iphone X 512 Gb', 'code' => 'APPLEX512', 'price' => '1349.9']
]);
$db->table('products')->upsert([
'name' => 'Apple Iphone X 128 Gb',
'code' => 'APPLEX128',
'price' => '999.9'
]);
$db->table('products')->insertIgnore([
'name' => 'Apple Iphone X 128 Gb',
'code' => 'APPLEX128',
'price' => '999.9'
]);
$db->table('products')->insertReplace([
'name' => 'Apple Iphone X 128 Gb',
'code' => 'APPLEX128',
'price' => '999.9'
]);
$update = $db->table('products')
->where('id', 11255)
->update(['active' => 1]);
$touch = $db->table('products')
->touch('active');
$increment = $db->table('posts')
->where('slug', 'whats-new-in-laravel-8')
->increment('hit');
$increment = $db->table('posts')
->where('slug', 'whats-new-in-laravel-8')
->decrement('hit');
$delete = $db->in('id', [321, 412, 324, 142])
->delete('products');
$db->table('users')->filter()->insert([
'username' => 'walterbishop',
'email' => '[email protected] ',
'password' => 'U7!hsjlIus',
'gender' => 'Male',
'fullname' => 'Walter Bishop'
]);
$db->table('users')->filter()->insert($_POST);
try{
$db->table('users')->validate()->insert([
'username' => 'walterbishop',
'email' => '[email protected] ',
'password' => 'U7!hsjlIus',
'gender' => 'Elephant'
]);
} catch(Exception $e){
echo $e->getMessage();
}
try {
$db->beginTransaction();
$db->table('products')->insert([
'name' => 'Apple Iphone X 128 Gb',
'code' => 'APPLEX128',
'price' => '999.9'
]);
$db->table('images')->insert([
'productId' => $db->lastInsertId(),
'imageName' => 'foo.jpg'
]);
$db->commit();
} catch(Exception $e) {
$db->rollBack();
}
$db->select('id, name, code, price')...
$db->sum('amount')...
$db->table('products')...
$db->table('products as p')
->leftJoin('images as i', 'p.id', 'i.productId')
->get();
$basketData = $db->table('users AS u')
->select('u.*')
->leftJoin('cart AS c', 'c.userId', 'u.id')
->joinNode('cartData', ['name' => 'c.productName', 'quantity' => 'c.quantity'])
->group('u.id')
->first();
stdClass Object
(
[id] => 159
[fullName] => John Doe
[email] => [email protected]
[cartData] => Array
(
[0] => stdClass Object
(
[name] => Apple Iphone X 128 GB
[quantity] => 1
)
[1] => stdClass Object
(
[name] => Apple Iphone X 256 GB
[quantity] => 1
)
)
)
$db->where('id', 32886)...
$db->table('products')
->like('name', '%iphone%')
->grouped(function($q){
$q->in('brandId', [1, 2, 3])->orIn('categoryId', [1, 2, 3]);
})->get();
$db->between('price', 50, 250)...
$db->isNull('code')...
$db->in('id', [33922, 31221, 45344, 35444])...
$db->findInSet('categoryId', 139)...
$db->like('name', '%Apple%')...
$db->order('id')...
$db->group('id')...
$db->having('stock', 5)...
$db->limit(100)...
$db->limit(100, 0)...
$db->limit(100)->offset(0)...
var_dump($db->queryHistory());
Array
(
[0] => Array
(
[query] => SELECT id, name FROM products WHERE code = ? AND active = ? ORDER BY id desc
[params] => Array
(
[0] => 34066
[1] => 1
)
[from] => redis
)
)
echo $db->lastQuery();
var_dump($db->lastParams());
Array
(
[0] => 34066,
[1] => 1
)
echo $db->queryCount();
$db->repair('sessions');
sql
SELECT
id, name, code, price, stock
FROM
products
WHERE
price BETWEEN ? AND ?
AND ((name LIKE ? OR code LIKE ?) OR featured=?)
AND categoryId IN(?,?,?,?,?,?)
ORDER BY
price DESC