PHP code example of kolaykod / simpledb
1. Go to this page and download the library: Download kolaykod/simpledb 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/ */
kolaykod / simpledb example snippets
$db = new \kolaykod\simpledb([
'database' => 'ecommerce',
'username' => 'root'
'password' => 'test',
'prefix' =>'prefix',
'charset' => 'utf8'
]);
$db = new \kolaykod\simpledb([
'driver' => 'sqlite',
'database' => 'ecommerce.sqlite'
]);
$db = new \kolaykod\simpledb([
'database' => 'ecommerce',
'username' => 'root',
'password' => 'test',
'charset' => 'utf8'
]);
[
'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;
}
$products = $db->select('id, name, code, slug, price, stock')
->table('products')
->where('stock > ?', 5)
->where('MONTH(created) = MONTH(NOW())')
->order('id')
->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');
$pluck = $db->table('products')
->pluck('name', 'code');
$user = $db->table('users')
->find(15);
$total = $db->table('users')
->where('userGroup', 'Admin')
->total();
echo $db->rowCount();
echo $db->lastInsertId();
$results = $db->raw('SELECT id FROM products WHERE active = ? AND MONTH(created) = MONTH(NOW())', 1)
->getCols();
$update = $db->raw('UPDATE payments SET active = !active WHERE status = ?', ['paid'])
->exec();
$posts = $db->table('posts')
->pager(25)
->get();
$db->pager(25, 'sayfa');
echo $db->pagerLinks();
var_dump($db->pagerData());
$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();
$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());
echo $db->lastQuery();
var_dump($db->lastParams());
echo $db->queryCount();
$db->repair('sessions');