Download the PHP package solophp/query-builder without Composer
On this page you can find all versions of the php package solophp/query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download solophp/query-builder
More information about solophp/query-builder
Files in solophp/query-builder
Package query-builder
Short Description A lightweight, fluent SQL query builder for PHP
License MIT
Informations about the package query-builder
Solo PHP Query Builder
A lightweight and flexible SQL query builder for PHP 8.2+ with support for multiple SQL dialects.
Features
- 🚀 Fast and lightweight SQL builder with zero external dependencies
- 💪 PHP 8.2+ support with strict typing
- 🔒 Secure parameterized queries for protection against SQL injections
- 🧩 Intuitive fluent interface for building queries
- 🔄 Support for different DBMS (MySQL, PostgreSQL, SQLite) with extensibility
- ⚡️ Optional PSR-16 caching of SELECT query results (local and global control)
- 🧩 Advanced features: subqueries, raw SQL expressions, conditional queries, DISTINCT selection
Installation
Quick Start
Caching (PSR-16)
This library supports caching SELECT query results via any PSR-16 cache implementation:
- Global cache: call
Query::enableCache($cache, $ttl)
once in your bootstrap to apply caching to all queries. - Per-instance cache: chain
->withCache($cache, $ttl)
on aQuery
instance to override or set caching locally. - Disable cache: use
Query::disableCache()
to turn off global caching.
Manual Initialization
If you need more control over the initialization process, you can create all components manually:
Building without Executing
You can also build queries without executing them:
Multi-DBMS Support
The library implements SQL grammar abstraction, allowing you to work with different database systems using the same API.
Setting Default DBMS
Specifying DBMS for a Query
SELECT Queries
Basic Selection Operations
Raw SQL Expressions
You can use raw SQL expressions by enclosing them in curly braces {...}
:
Conditional Queries with when()
The when()
method allows you to add clauses to your query conditionally:
Complex Conditions
JOIN Operations
Grouping and Aggregation
INSERT Queries
UPDATE Queries
DELETE Queries
Checking for Records
Query Execution
The library provides a flexible mechanism for executing queries using the PDO executor or your own custom executor:
Using QueryFactory Utility
The simplest way to initialize is with the QueryFactory
utility class:
Manual Configuration
For more control over the configuration:
API Reference
Query Methods
Method | Description |
---|---|
from(string $table) |
Sets the table to select from |
select(string ...$columns) |
Sets the columns to select |
distinct(bool $value = true) |
Enables or disables DISTINCT selection |
insert(string $table) |
Starts an insert query |
update(string $table) |
Starts an update query |
delete(string $table) |
Starts a delete query |
setDatabaseType(string $type) |
Sets the database type (mysql, postgresql, sqlite) |
Where Conditions
Method | Description |
---|---|
where(string\|\Closure $expr, mixed ...$bindings) |
Adds a WHERE condition |
orWhere(string\|\Closure $expr, mixed ...$bindings) |
Adds an OR WHERE condition |
andWhere(string\|\Closure $expr, mixed ...$bindings) |
Adds an AND WHERE condition |
when(bool $condition, callable $callback, ?callable $default = null) |
Conditionally adds clauses |
Joins
Method | Description |
---|---|
join(string $table, string $condition, mixed ...$bindings) |
Adds an INNER JOIN |
leftJoin(string $table, string $condition, mixed ...$bindings) |
Adds a LEFT JOIN |
rightJoin(string $table, string $condition, mixed ...$bindings) |
Adds a RIGHT JOIN |
fullJoin(string $table, string $condition, mixed ...$bindings) |
Adds a FULL OUTER JOIN |
joinSub(\Closure $callback, string $alias, string $condition, mixed ...$bindings) |
Adds a subquery join |
Clauses
Method | Description |
---|---|
groupBy(string ...$cols) |
Adds a GROUP BY clause |
having(string\|\Closure $expr, mixed ...$bindings) |
Adds a HAVING clause |
orHaving(string\|\Closure $expr, mixed ...$bindings) |
Adds an OR HAVING clause |
orderBy(string $column, string $direction = 'ASC') |
Sets the ORDER BY clause |
addOrderBy(string $column, string $direction = 'ASC') |
Adds an additional ORDER BY clause |
limit(int $limit, ?int $offset = null) |
Adds a LIMIT clause |
Count Methods
Method | Description |
---|---|
count(?string $column = null, bool $distinct = false) |
Counts records |
Insert Methods
Method | Description |
---|---|
values(array $data) |
Sets values for insert |
insertGetId() |
Executes the insert and returns the last insert ID |
execute() |
Executes the insert and returns the number of affected rows |
Update Methods
Method | Description |
---|---|
set(string\|array $column, mixed $value = null) |
Sets the column(s) to update |
execute() |
Executes the update and returns the number of affected rows |
Select Result Methods
Method | Description |
---|---|
getAssoc() |
Fetches a single row as an associative array |
getAllAssoc() |
Fetches all rows as associative arrays |
getObj(string $className = 'stdClass') |
Fetches a single row as an object |
getAllObj(string $className = 'stdClass') |
Fetches all rows as objects |
getValue() |
Fetches a single value from the first column |
getColumn(string $column, ?string $keyColumn = null) |
Fetches an array of values from a single column |
paginate(int $limit, int $page = 1) |
Sets pagination with page number (using LIMIT and OFFSET) |
exists() |
Checks if any rows exist |
count(?string $column = null, bool $distinct = false) |
Counts records that match the query |
build() |
Returns the SQL and bindings without executing |
Extending Functionality
Adding Support for a New DBMS
To add support for a new DBMS:
- Create a new grammar class inheriting from
AbstractGrammar
- Add the new DBMS to
GrammarFactory
Requirements
- PHP 8.2 or higher
- PDO Extension (for database connections)
License
MIT