1. Go to this page and download the library: Download finesse/mini-db 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/ */
finesse / mini-db example snippets
$database = Database::create([
'driver' => 'mysql',
'dsn' => 'mysql:host=localhost;dbname=my_database',
'username' => 'root',
'password' => 'qwerty',
'prefix' => 'test_'
]);
$database->statement('
CREATE TABLE '.$database->addTablePrefix('users').' (
id INT(11) NOT NULL AUTO_INCREMENT,
email VARCHAR(50) NOT NULL,
account INT(11) NOT NULL DEFAULT 0
)
');
$database->table('users')->insert([
['name' => 'Jack', 'account' => 1200],
['name' => 'Bob', 'account' => 500],
['name' => 'Richard', 'account' => 800]
]);
$database->table('users')->where('account', '>', 600)->get(); // Jack and Richard
use Finesse\MiniDB\Database;
$database = Database::create([
'driver' => 'mysql', // DBMS type: 'mysql', 'sqlite' or anything else for other (optional)
'dsn' => 'mysql:host=host;dbname=db', // PDO data source name (DSN)
'username' => 'root', // Database username (optional)
'password' => 'qwerty', // Database password (optional)
'options' => [], // PDO options (optional)
'prefix' => '' // Tables prefix (optional)
]);
use Finesse\MicroDB\Connection;
use Finesse\MiniDB\Database;
use Finesse\QueryScribe\Grammars\MySQLGrammar;
use Finesse\QueryScribe\PostProcessors\TablePrefixer;
$connection = Connection::create('mysql:host=host;dbname=db', 'username', 'password');
$grammar = new MySQLGrammar();
$tablePrefixer = new TablePrefixer('demo_');
$database = new Database($connection, $grammar, $tablePrefixer);
$database->insertGetId('INSERT INTO users (name, email) VALUES (?, ?), (?, ?)', ['Ann', '[email protected]', 'Bob', '[email protected]']); // 19 (the last inserted row id)
$database->select('SELECT * FROM users WHERE name = ? OR email = ?', ['Jack', '[email protected]']);
/*
[
['id' => 4, 'name' => 'Jack', 'email' => '[email protected]', 'account' => 1230],
['id' => 17, 'name' => 'Bill', 'email' => '[email protected]', 'account' => -100]
]
*/
$database->import('path/to/file.sql');
$database->select('SELECT * FROM '.$database->addTablePrefix('users').' ORDER BY id');
use Finesse\MiniDB\ThirdParty\PagerfantaAdapter;
use Pagerfanta\Pagerfanta;
$paginator = new Pagerfanta(new PagerfantaAdapter($query));
$paginator->setMaxPerPage(10); // The number of rows on a page
$paginator->setCurrentPage(3); // The current page number
$currentPageRows = $paginator->getCurrentPageResults(); // The rows for the current page
$pagesCount = $paginator->getNbPages(); // Total pages count
$haveToPaginate = $paginator->haveToPaginate(); // Whether the number of results is higher than the max per page
$database
->table('users')
->orderBy('id')
->chunk(100, function ($users) {
foreach ($users as $user) {
// Process a row here
}
});