PHP code example of silverslice / easydb

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

    

silverslice / easydb example snippets


use Silverslice\EasyDb\Database;

// connect options
$options = [
    'host'     => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'testdb',
    'charset'  => 'utf8'
];

// mysql options (not 

$db->getAll('SELECT * FROM table WHERE id IN (?a)', [1, 2, 3]);

// generated sql: SELECT * FROM table WHERE id IN (1, 2, 3)

$db->query('UPDATE products SET ?u', ['key' => 'foo', 'value' => 1]);

// generated sql: UPDATE products SET `key` = 'foo', `value` = 1

$pricePart = '';
if ($price) {
    $pricePart = $db->prepare(' AND price < ?', $price);
}

$db->getAll('SELECT * FROM products WHERE category_id = ? ?p', $categoryId, $pricePart);

try {
    $this->db->query("SELECTT 1");
} catch (Exception $e) {
    // get error code
    $code = $e->getCode();

    // get query with error
    $query = $e->getQuery();

    // get error message: code, error and query
    $message = $e->getMessage();
}

getOne($query, $param1, $param2...)

getAssoc($query, $param1, $param2...)

getAll($query, $param1, $param2...)

getColumn($query, $param1, $param2...)

getPairs($query, $param1, $param2...)

getAllKeyed($query, $param1, $param2...)

insert($table, $params, $ignore = false)

update($table, $params, $where = array())

insertUpdate($table, $insert, $update = array())

multiInsert($table, $fields, $data, $ignore = false)

delete($table, $where = array())

beginTransaction()

commit()

rollback()

transaction($process)

$this->db->transaction(function () {
    $this->db->query("INSERT INTO test (id, code, price) VALUES (3003, '1', 1)");
    $this->db->query("INSERT INTO test (id, code, price) VALUES (3004, '1', 1)");
});

$this->db->insert('test', [
    'time' => new Expression('NOW()')
]);

// or use expression alias

$this->db->insert('test', [
    'time' => $this->db->expression('NOW()')
]);

multiQuery($queries)

$this->db->multiQuery("
    DROP TABLE IF EXISTS `test`;
    CREATE TABLE `test` (
     `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
     `code` char(15) NOT NULL,
     `name` varchar(200) NOT NULL,
     `price` decimal(10,2) unsigned DEFAULT NULL,
     `order` int(11) unsigned DEFAULT 0,
     PRIMARY KEY (`id`),
     KEY `code` (`code`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO test (id, code, name, price)
    VALUES
      (1, '001', 'Cup', 20.00),
      (2, '002', 'Plate', 30.50)
");