PHP code example of timmit / db-mysql

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

    

timmit / db-mysql example snippets


use TiMMiT\MySQL\DB;

// Connect. `host`, `username`, `password`, `database` and `port` are used.
new DB([
    'host'     => '127.0.0.1',
    'username' => 'app',
    'password' => 'secret',
    'database' => 'app',
    'port'     => 3306,
]);

// Select rows, optionally keyed by a column.
$users = DB::selectQuery('SELECT id, name FROM users WHERE active = 1', 'id');

// Insert, update and delete.
$id = DB::insertQuery("INSERT INTO users (name) VALUES ('" . DB::clean4DB($name) . "')");
DB::updateQuery("UPDATE users SET active = 0 WHERE id = " . (int)$id);
DB::deleteQuery('DELETE FROM users WHERE id = ' . (int)$id);

use TiMMiT\MySQL\DB;
use TiMMiT\MySQL\Where\Collection;

$where = new Collection('AND');
$where->addParam('status')->is('active');
$where->addParam('age')->greaterOrEqual(18);
$where->addParam('country')->inArray(['NL', 'BE']);

$rows = DB::selectQuery('SELECT * FROM users WHERE ' . DB::getWhereFromCollection($where));

DB::startTransaction();

try {
    DB::executeQuery('UPDATE accounts SET balance = balance - 10 WHERE id = 1');
    DB::executeQuery('UPDATE accounts SET balance = balance + 10 WHERE id = 2');
    DB::endTransaction(true);   // commit
} catch (\Throwable $e) {
    DB::endTransaction(false);  // rollback
    throw $e;
}