PHP code example of kecik / database

1. Go to this page and download the library: Download kecik/database 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/ */

    

kecik / database example snippets


$app->config->set('database.driver', 'mysqli');
$app->config->set('database.hostname', 'localhost');
$app->config->set('database.username', 'root');
$app->config->set('database.password', '1234567890');
$app->config->set('database.dbname', 'kecik');

$app->config->set('database.driver', 'pgsql');
$app->config->set('database.hostname', 'localhost');
$app->config->set('database.username', 'postgres');
$app->config->set('database.password', '1234567890');

$app->config->set('database.driver', 'pgsql');
$app->config->set('database.dsn', "host=localhost port=5432 dbname=kecik user=postgres password=1234567890 options='--client_encoding=UTF8'");

$app->config->set('database.driver', 'oci8');
$app->config->set('database.dsn', 'localhost/xe');
$app->config->set('database.username', 'kecik');
$app->config->set('database.password', '1234567890');

$app->config->set('database.driver', 'mongo');
$app->config->set('database.dsn', 'mongodb://localhost');
$app->config->set('database.dbname', 'kecik');

$app->config->set('database.driver', 'pdo');
$app->config->set('database.dsn', 'mysql:host=localhost;dbname=kecik;');
$app->config->set('database.username', 'root');
$app->config->set('database.password', '1234567890');

$app->config->set('database.driver', 'pdo');
$app->config->set('database.dsn', 'pgsql:host=localhost;dbname=kecik;');
$app->config->set('database.username', 'postgres');
$app->config->set('database.password', '1234567890');

$app->config->set('database.driver', 'pdo');
$app->config->set('database.dsn', 'oci:host=localhost;dbname=xe;');
$app->config->set('database.username', 'kecik');
$app->config->set('database.password', '1234567890');

$app->db->$table->insert($data);

$data = [
	'field_nama' => 'Dony Wahyu Isp',
	'field_email' => '[email protected]'
];

$app->db->$table->update($key, $data);

//** $key
$key = ['id' => 2];

//** $data
$data = [
	'field_nama' => 'dnaextrim',
	'field_email' => '[email protected]'
];

$app->db->$table->delete($key);

$key = ['id' => 3];

$rows = $app->db->$table->find($filter, $limit, $order_by);

$rows = $app->db->$table->find([
	'select' => [
		['nama, email'], //** Cara Pertama
		['nama', 'email'], //** Cara Kedua
		['max'=>'nilai'], //** Cara Ketiga
		['max'=>'nilai', 'as'=>'nilai_maksimum'] //** Cara Keempat
	]
]);

$rows = $app->db->$table->find([],[10]); //** Cara Pertama limit 10 baris
$app->db->$table->find([], [5, 10]); //** Cara Kedua limit dari posisi index ke 5 sebanyak 10 baris

$rows = $app->db->$table->find([],[],[
	'asc' => ['nama', 'email'], //** Pengurutan menaik/Ascending untuk field nama dan email
	'desc' => ['nama', 'email'] //** Pengurutan menurun/Descending untuk field nama dan email
]);

$rows = $app->db->$table->find([
	'where'=> [
		["nama = 'Dony Wahyu Isp'"], //** Cara Pertama
		["nama", "='Dony Wahyu Isp'"], //** Cara Kedua
		["nama", "=", "Dony Wahyu Isp"], //** Cara Ketiga
		["nama = '?' AND email = '?'" => [$nama, $email]], //** Cara Keempat
		["nama", "='?' AND email = '?'" => [$nama, $email]], //** Cara Kelima
	]
]);

$rows = $app->db->$table->find([
	'where' => [
		'and' => [
			'and' => [
				["nama", "=", "Dony Wahyu Isp"],
				["email", "=", "[email protected]"]
			],
			'or' => [
				["nama", "=", "Dony Wahyu Isp"],
				["email", "=", "[email protected]"]
			]
		]
	]
]);

$rows = $app->db->$table->find([
	'where' => [
		["nilai", "between", [50, 100]],
		["nilai", "not between", [50, 100]], //** Dengan NOT
	]
]);

$rows = $app->db->$table->find([
	'where' => [
		["nilai", "in", [50, 60, 70, 80]],
		["nilai", "not in", [50, 60, 70, 80]], //** Dengan NOT
	]
]);

$rows = $app->db->$table->find([
	'group by'=> [
		['username']
	]
]);

$rows = $app->db->$table->find([
	'join' => [
		['natural', 'table_profil'], //** Natural JOIN
		['left', 'table_profil', 'field_nama'], //** Left/Righ Join Cara Pertama
		['left', 'table_profil', ['field_nama_profile', 'field_nama_user']] //** Left/Right Join Cara Kedua
	]
]);

$rows = $app->db->$table->find([
	'callback'=> function($value, $row) {
		return 'Rp. '.$value;
	}
]);

$rows = $app->db->$table->find([
	'callback'=> [
		'harga' => function($value, $row) {
			return 'Rp. '.$value;
		},
		'password' => function($value, $row) {
			return '*****';
		},
		'id' => function($value, $row) {
			$row->action = "{\"id\":\"$value\"}";
			return $value;
		},
	]
]);

$rows = $app->db->$table->find();
$fields = $app->db->$table->fields();
foreach($fields as $field) {
	echo 'Name: '.$field->name;
	echo 'Type: '.$field->type;
	echo 'Size: '.$field->size;
	echo 'Table: '.$field->table;
}

$fields = $app->db->$table->fields();
foreach($fields as $field) {
	echo 'Name: '.$field->name;
	echo 'Type: '.$field->type;
	echo 'Size: '.$field->size;
	echo 'Table: '.$field->table;
}

$rows = $app->db->$table->find();
$count = $app->db->$table->num_rows();


fig = [
	'libraries' => [
		'database' => [
			'enable' => TRUE,
			'config' => [
				'driver' => 'mysqli',
				'hostname' => 'localhost',
				'username' => 'root',
				'password' => '1234567890',
				'dbname' => 'kecik'
			]
		]
	]
];

$app = new Kecik\Kecik($config);
$con = $app->db->connect();

$res = $app->db->exec("SELECT * FROM data", 'data');
print_r($app->db->fetch($res));

$id = ['id'=>'2'];
$data = [
	'nama'=>'Dony Wahyu Isp',
	'email'=>'[email protected]'
];

$db = $app->db;
$ret = $db->data->insert($data);
$ret = $db->data->update($id, $data);
$ret = $db->data->delete($id);

$app->get('/', function() use ($db){
	$rows = $db->data->find([
		'where' => [
			['nama', '=', "Dony Wahyu Isp"]
		],
		'callback' => [
			//manipulating value of email fields
			'email' => function($val, $row) {
				return substr($val, 0, 3).str_repeat('*', strpos($val, '@')-3).substr($val, strpos($val, '@'))
			}
		]
	]);
	
	foreach ($rows as $row) {
		echo 'Nama: '.$row->nama.'<br />';
		/* 
		Output email for [email protected]
		is dna*******@gmail.com
		*/
		echo 'Email: '.$row->email.'<hr />';
	}
});

$app->run();


$app = new Kecik\Kecik();

$app->config->set('database.driver', 'mysqli');
$app->config->set('database.hostname', 'localhost');
$app->config->set('database.username', 'root');
$app->config->set('database.password', '1234567890');
$app->config->set('database.dbname', 'kecik');

$db = new Kecik\Database($app);
$con = $db->connect();

$res = $db->exec("SELECT * FROM data", 'data');
print_r($db->fetch($res));

$id = ['id'=>'2'];
$data = [
	'nama'=>'Dony Wahyu Isp',
	'email'=>'[email protected]'
];

$ret = $db->data->insert($data);
$ret = $db->data->update($id, $data);
$ret = $db->data->delete($id);

$app->get('/', function() use ($db){
	$rows = $db->data->find([
		'where' => [
			['nama', '=', "Dony Wahyu Isp"]
		],
		'callback' => [
			//manipulating value of email fields
			'email' => function($val, $row) {
				return substr($val, 0, 3).str_repeat('*', strpos($val, '@')-3).substr($val, strpos($val, '@'))
			}
		]
	]);
	
	foreach ($rows as $row) {
		echo 'Nama: '.$row->nama.'<br />';
		/* 
		Output email for [email protected]
		is dna*******@gmail.com
		*/
		echo 'Email: '.$row->email.'<hr />';
	}
});

$app->run();