PHP code example of hunnomad / classdatabase

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

    

hunnomad / classdatabase example snippets


use HunNomad\Database\Database;

$db = new Database('localhost','example','root','pass','3306','mysql');
$pdo = $db->getConnection(); // native PDO

$id = $db->insert('users', [
  'name' => 'John Doe',
  'email'=> '[email protected]'
]);

$rows = $db->select('users', ['status'=>1], [
  'order'=>'id DESC',
  'limit'=>20
]);

$db->update('users', ['status'=>0], ['id'=>5]);

$db->delete('users', ['id'=>5]);

$rows = $db->rawQuery(
  "SELECT * FROM users WHERE email = :email",
  ['email' => '[email protected]']
);

$affected = $db->rawQuery(
  "UPDATE users SET last_login = NOW() WHERE id=:id",
  ['id'=>5]
);

$db->begin();
try {
    $db->insert('logs', ['msg'=>'Test']);
    $db->update('users', ['active'=>1], ['id'=>10]);
    $db->commit();
} catch (Throwable $e) {
    $db->rollback();
}

$db = new Database('localhost','mydb','user','pass','27017','mongodb');

$id = $db->insert('users', ['name'=>'Sarah']);
$docs = $db->select('users', ['name'=>'Sarah']);
$db->update('users', ['active'=>true], ['name'=>'Sarah']);
$db->delete('users', ['active'=>false]);

$db = new Database('localhost','', '', 'pass','6379','redis');
$redis = $db->getConnection();

$redis->set('foo','bar');
echo $redis->get('foo');

classdatabase/
├─ src/Database.php
├─ examples/
│  └─ example.php
├─ README.md
├─ CHANGELOG.md
├─ composer.json
└─ LICENSE