1. Go to this page and download the library: Download solophp/query-builder 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/ */
use Solo\QueryBuilder\Facade\Query;
use ExampleCache;
// Global cache for all Query instances (TTL 600s)
Query::enableCache(new ExampleCache(), 600);
// Local override (TTL 300s)
$query = $query->withCache(new ExampleCache(), 300);
$users = $query->from('users')->getAllAssoc();
// Disable global cache entirely
Query::disableCache();
use Solo\QueryBuilder\Facade\Query;
use Solo\QueryBuilder\Executors\PdoExecutor\PdoExecutor;
use Solo\QueryBuilder\Executors\PdoExecutor\Connection;
use Solo\QueryBuilder\Executors\PdoExecutor\Config;
use Solo\QueryBuilder\Factory\BuilderFactory;
use Solo\QueryBuilder\Factory\GrammarFactory;
// Create factories
$grammarFactory = new GrammarFactory();
// Create PDO executor
$config = new Config(
'localhost', // host
'username', // username
'password', // password
'database', // database
PDO::FETCH_ASSOC, // fetchMode
'mysql', // driver
null, // port (optional)
[] // options (optional)
);
$connection = new Connection($config);
$executor = new PdoExecutor($connection);
// Creating a BuilderFactory with executor
$builderFactory = new BuilderFactory($grammarFactory, $executor, 'mysql');
// Creating a Query instance
$query = new Query($builderFactory);
// Build a query without executing
[$sql, $bindings] = $query->from('users')
->select('id', 'name')
->where('status = ?', 'active')
->build();
// Now you have the SQL string and parameter bindings
echo $sql;
// SELECT `id`, `name` FROM `users` WHERE status = ?
print_r($bindings);
// ['active']
// Set MySQL as default grammar
$query->setDatabaseType('mysql');
// Set PostgreSQL as default grammar
$query->setDatabaseType('postgresql'); // or 'postgres', 'pgsql'
// Set SQLite as default grammar
$query->setDatabaseType('sqlite');
// GROUP BY with aggregate functions
$userOrderStats = $query->from('orders')
->select('user_id', '{COUNT(*) as order_count}', '{SUM(amount) as total_spend}')
->groupBy('user_id')
->having('total_spend > ?', 1000)
->getAllAssoc();
// Insert one record and get ID
$userId = $query->insert('users')
->values([
'name' => 'John Doe',
'email' => '[email protected]',
'created_at' => date('Y-m-d H:i:s')
])
->insertGetId();
// Insert one record and get affected rows
$affectedRows = $query->insert('users')
->values([
'name' => 'John Doe',
'email' => '[email protected]',
'created_at' => date('Y-m-d H:i:s')
])
->execute();
// Insert multiple records
$affectedRows = $query->insert('logs')
->values([
['user_id' => 1, 'action' => 'login', 'created_at' => date('Y-m-d H:i:s')],
['user_id' => 2, 'action' => 'logout', 'created_at' => date('Y-m-d H:i:s')]
])
->execute();
// Delete with condition
$affectedRows = $query->delete('expired_tokens')
->where('expires_at < ?', date('Y-m-d H:i:s'))
->execute();
// Delete by ID
$affectedRows = $query->delete('users')
->where('id = ?', 5)
->execute();
// Check if records exist
$exists = $query->from('users')
->where('status = ?', 'active')
->exists();
use Solo\QueryBuilder\Utility\QueryFactory;
// Create a Query instance with one line
$query = QueryFactory::createWithPdo(
'localhost', // host
'db_user', // username
'db_password', // password
'database_name', // database
PDO::FETCH_ASSOC, // fetchMode
'mysql', // optional database type
null, // optional port
[] // optional PDO options
);
// Execute query and get results
$users = $query->from('users')
->select('id', 'name')
->where('status = ?', 'active')
->getAllAssoc();
use Solo\QueryBuilder\Executors\PdoExecutor\PdoExecutor;
use Solo\QueryBuilder\Executors\PdoExecutor\Connection;
use Solo\QueryBuilder\Executors\PdoExecutor\Config;
use Solo\QueryBuilder\Factory\BuilderFactory;
use Solo\QueryBuilder\Factory\GrammarFactory;
use Solo\QueryBuilder\Facade\Query;
// Configure PDO connection
$config = new Config(
'localhost', // host
'db_user', // username
'db_password', // password
'database_name', // database
PDO::FETCH_ASSOC, // fetchMode
'mysql', // driver
3306, // port (optional)
[] // options (optional)
);
// Create connection and executor
$connection = new Connection($config);
$executor = new PdoExecutor($connection);
// Create factories
$grammarFactory = new GrammarFactory();
$builderFactory = new BuilderFactory($grammarFactory, $executor, 'mysql');
// Create query builder
$query = new Query($builderFactory);
namespace Solo\QueryBuilder\Grammar;
final class CustomGrammar extends AbstractGrammar
{
protected string $tableQuote = '`';
protected string $columnQuote = '`';
// No need to implement any methods as they are already in AbstractGrammar
// Just override the quote characters as needed
}
// Then update GrammarFactory to recognize your new dialect
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.