PHP code example of eznio / db

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

    

eznio / db example snippets


// Initializing database-specific driver
$driver = new \eznio\db\drivers\Sqlite('db/test.sqlite3');

// Creating Entity Manager
$em = new \eznio\db\EntityManager($driver);

// Getting query repository by table name
$repo = $em->getRepository('test');

// Getting ActiveRecord entity of table row with id = 1
$entity =$repo->findOneById(1);

// Updating and saving entity
$entity->field = 'new value';
$entity->save();

   $driver->query(
       'SELECT * FROM USERS WHERE name = ? AND age = ?',
       ['John', 26]
   );
   

   $driver->query(
       'SELECT * FROM USERS WHERE name = :name: AND age = :age:',
       ['age' => 26, 'name' => 'John']
   );
   

public function query($sql, array $args = []);

$driver->query("SET NAMES :encoding:", ['encoding' => 'UTF-8'];

public function select($sql, array $args = []);

$result => $driver->select("SELECT * FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      ['id' => 1, 'name' => 'John', 'surname' => 'Smith'],
 *      ['id' => 2, 'name' => 'John', 'surname' => 'Doe']
 */ ];

$result => $driver->select("SELECT id AS ARRAY_KEY, * FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      1 => ['name' => 'John', 'surname' => 'Smith'],
 *      2 => ['name' => 'John', 'surname' => 'Doe']
 */ ];

public function getRow($sql, array $args = []);

$result => $driver->getRow("SELECT * FROM users WHERE name = ?", ['John']);

public function getColumn($sql, array $args = []);

$result => $driver->getColumn("SELECT id FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      1,
 *      2
 */ ];

$result => $driver->getColumn("SELECT id AS ARRAY_KEY, surname FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      1 => 'Smith',
 *      2 => 'Doe'
 */ ];

public function getCell($sql, array $args = []);

$result => $driver->getCell("SELECT COUNT(*) FROM users WHERE name = ?", ['John']);

//  $result = 2;

public function load($table, $id);

$result => $driver->load('users', 1);

//  $result = ['id' => 1, name' => 'John', 'surname' => 'Smith'];

public function insert($table, array $data);

$result => $driver->insert('users', [
    'name' => 'John',
    'surname' => 'McKey'
]);

//  $result = 3;

public function update($table, $id, $data);

$driver->update('users', [
    'name' => 'Mike',
], 1);

public function delete($table, $id);

$driver->delete('users', 3);