1. Go to this page and download the library: Download inclitoleo/dbmysql 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/ */
inclitoleo / dbmysql example snippets
use Inclitoleo\Mysql\Connection\ConnectionConfig;
use Inclitoleo\Mysql\Connection\ConnectionManager;
use Inclitoleo\Mysql\Query\QueryBuilder;
use Inclitoleo\Mysql\Security\SchemaRegistry;
$manager = new ConnectionManager(new ConnectionConfig(
host: '127.0.0.1',
port: 3306,
database: 'app',
username: 'app',
password: 'secret',
debug: false,
));
$schema = new SchemaRegistry();
$schema->register('account', ['id', 'name', 'email']);
$schema->register('orders', ['id', 'account_id', 'total']);
$schema->register('logs', ['id', 'message', 'created_at']);
$builder = new QueryBuilder($schema, $manager);
use Cake\Core\Configure;
use Inclitoleo\Mysql\Connection\ConnectionConfig;
use Inclitoleo\Mysql\Connection\ConnectionManager;
use Inclitoleo\Mysql\Query\QueryBuilder;
use Inclitoleo\Mysql\Security\SchemaRegistry;
$config = ConnectionConfig::fromArray(Configure::read('Datasources.default'));
$schema = new SchemaRegistry();
$schema->register('account', ['id', 'name', 'email']);
$builder = new QueryBuilder($schema, new ConnectionManager($config));
$accounts = $builder->raw('SELECT * FROM account');
public function index(QueryBuilder $db): JsonResponse
{
return $this->json($db->raw('SELECT * FROM account'));
}
$accounts = $builder->raw('SELECT * FROM account');
$one = $builder->raw('SELECT * FROM account WHERE id = ?', [1]);
$builder->raw('UPDATE account SET name = ? WHERE id = ?', ['Ada', 1]);
$rows = $builder->raw(
'SELECT a.id, a.name, o.total
FROM account a
INNER JOIN orders o ON o.account_id = a.id
WHERE a.id = ?',
[1],
);
$left = $builder->raw(
'SELECT a.name, o.total
FROM account a
LEFT JOIN orders o ON o.account_id = a.id',
);
$rows = $builder->raw(
'WITH recent AS (
SELECT account_id, SUM(total) AS spent
FROM orders
GROUP BY account_id
)
SELECT a.name, r.spent
FROM account a
INNER JOIN recent r ON r.account_id = a.id',
);
$recent = (new QueryBuilder($schema))
->from('orders')
->select(['account_id', 'total']);
$rows = $builder
->with('recent', $recent)
->from('recent')
->select(['account_id', 'total'])
->get();
// string subquery also works
$rows = $builder
->with('recent', 'SELECT account_id, SUM(total) AS spent FROM orders GROUP BY account_id')
->from('recent')
->select(['account_id', 'spent'])
->get();
$rows = $builder
->from('account')
->select(['id', 'name'])
->rowNumber('rank', partitionBy: 'id', orderBy: 'id')
->get();
// SELECT id, name, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS rank FROM account
$builder->from('account')->select(['id', 'name']); // ok
$builder->raw('SELECT * FROM account'); // ok
$builder->from('account; DROP TABLE users'); // InvalidIdentifierException
$builder->raw('SELECT * FROM account; DROP TABLE account'); // InvalidIdentifierException
$manager = new ConnectionManager($config, logger: $psrLogger);
use Inclitoleo\Mysql\database\MySqlClient;
$db = new MySqlClient();
$id = $db->insert('account', $obj);
const INCLITOSTRICT = true;
// before
$db = new MySqlClient();
$row = $db->select('account', 'id', 1);
$all = $db->select_all('SELECT * FROM account WHERE id = 1', 'A');
// after — same SQL you already had
$all = $builder->raw('SELECT * FROM account WHERE id = ?', [1]);
// or the builder, if you prefer
$row = $builder->from('account')->where('id', '=', 1)->first();