PHP code example of ifcanduela / db

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

    

ifcanduela / db example snippets




use ifcanduela\db\Database;

$sqlite = Database::sqlite($filename, $options);
$mysql  = Database::mysql($host, $dbname, $user, $password, $options);

$mysql = Database::fromArray([
        'engine' => 'mysql',
        'host' => '127.0.0.1',
        'name' => 'some_database',
        'user' => 'some_username',
        'pass' => 'some_password',
    ]);

$sqlite = Database::fromArray([
        'engine' => 'sqlite',
        'file' => './db.sqlite',
    ]);




use ifcanduela\db\Query;

$query = Query::select()
    ->columns('users.*')
    ->from('users')
    ->leftJoin('profiles', ['users.id' => 'profiles.user_id'])
    ->where(['status' => ['<>', 1]])
    ->orderBy('created DESC', 'username')
    ->limit(1, 2);

echo $query; // or $query->getSql();
// SELECT users.*
// FROM users LEFT JOIN profiles ON users.id = profiles.user_id
// WHERE status <> :_param_1
// ORDER BY created DESC, username
// LIMIT 2, 1;

$sqlite->run($query);

$sqlite->query($query->getSql(), $query->getParams());

use ifcanduela\db\Database;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('Query log');
$file_handler = new StreamHandler('queries.log', Logger::INFO);
$logger->pushHandler($file_handler);

$db = Database::sqlite(':memory');
$db->setLogger($logger);

$db->run('SELECT 1');

Query::select(string ...$field)
    ->distinct(bool $enable = true)
    ->columns(string ...$column)
    ->from(string ...$table)
    ->join(string $table, array $on)
    ->innerJoin(string $table, array $on)
    ->leftJoin(string $table, array $on)
    ->leftOuterJoin(string $table, array $on)
    ->rightJoin(string $table, array $on)
    ->outerJoin(string $table, array $on)
    ->fullOuterJoin(string $table, array $on)
    ->where(array $conditions)
    ->andWhere(array $conditions)
    ->orWhere(array $conditions)
    ->groupBy(string ...$field)
    ->having(array $conditions)
    ->andHaving(array $conditions)
    ->orHaving(array $conditions)
    ->orderBy(string ...$field)
    ->limit(int $limit, int $offset = null)
    ->offset(int $offset)
    ->getSql()
    ->getParams()

Query::insert(string $table = null)
    ->table(string $table)
    ->into(string $table)
    ->values(array ...$values)
    ->getSql()
    ->getParams()

Query::update(string $table = null)
    ->table(string $table)
    ->set(array $values)
    ->where(array $conditions)
    ->andWhere(array $conditions)
    ->orWhere(array $conditions)
    ->getSql()
    ->getParams()

Query::delete(string $table = null)
    ->table(string $table)
    ->where(array $conditions)
    ->andWhere(array $conditions)
    ->orWhere(array $conditions)
    ->getSql()
    ->getParams()

$q = Query::select();

$q->columns('id', 'name', 'age');
$q->from('users');
$q->where(['id' => 1]);
$q->orWhere(['id' => 3]);
$q->andWhere(['age' => ['>', 18]]);
$q->orderBy('age DESC');

[
    ":p_1" => 1,
    ":p_2" => 3,
    ":p_3" => 18,
]

$q = Query::select()->where([
        'AND',
        'a' => 1,
        'b' => 2,
        [
            'OR',
            'c' => 3,
            'd' => 4,
        ]
    ]);