Download the PHP package buildql/query-builder without Composer

On this page you can find all versions of the php package buildql/query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package query-builder

BuildQL Query Builder

A secure, fluent, and lightweight SQL Query Builder for PHP 8.0+, inspired by Laravel's Query Builder. BuildQL provides an expressive and intuitive API for building complex database queries without writing raw SQL.

PHP Version

🎉 What's New in v1.0.4

View full changelog


Features

Requirements

Installation

Install via Composer:

Quick Start

Basic Configuration

Using .env File (Recommended)

Create a .env file in your project root directory:

Bootstrap the connection:


API Reference

Database Connection Methods

DB::setConnection()

Manually establish a database connection.

Parameters:

Example:

Note: Connection can only be established at once. Call DB::resetConnection() first to reconnect.


DB::boot()

Load database credentials from .env file and establish connection.

Parameters:

Requirements:

Example:

Throws: BuilderException if credentials are missing or invalid.


DB::table()

Create a new query builder instance for a specific table.

Parameters:

Example:


DB::raw()

Execute raw SQL queries with parameter binding.

Parameters:

Returns:

Example:

Note: Only positional placeholders allowed, not named placeholders.


DB::setDatabaseGlobally()

Set the default database for all queries.

Example:


DB::getGlobalDatabase()

Get the currently selected global database name.

Returns: Database name or null if not set

Example:


DB::resetConnection()

Close the current database connection.

Example:


Query Builder Methods

All query builder methods return $this for method chaining unless otherwise specified.


select()

Specify columns to retrieve.

Parameters:

Column Formats:

Column structure:

Examples:


selectAggregate()

Add aggregate functions to the query.

Parameters: Each parameter accepts 'column' or 'column:alias' format.

Examples:


distinct()

Return only unique rows.

Example:


where()

Add a WHERE condition to filter results.

Parameters:

Supported Operators: =, !=, >, <, >=, <=, LIKE, NOT LIKE

Examples:


orWhere()

Add an OR WHERE condition.

Example:

Note: AND has higher precedence than OR in MySQL. For complex conditions, be aware of operator precedence.


whereIn()

Filter by a set of values.

Example:


orWhereIn()

Add an OR WHERE IN condition.

Example:


whereNotIn()

Exclude records matching a set of values.

Example:


orWhereNotIn()

Add an OR WHERE NOT IN condition.

Example:


whereNull()

Filter records where a column is NULL.

Example:


orWhereNull()

Add an OR WHERE NULL condition.

Example:


whereNotNull()

Filter records where a column is NOT NULL.

Example:


orWhereNotNull()

Add an OR WHERE NOT NULL condition.

Example:


join()

Join tables using INNER JOIN.

Parameters:

Examples:


leftJoin()

Perform a LEFT JOIN.

Example:


rightJoin()

Perform a RIGHT JOIN.

Example:


crossJoin()

Perform a CROSS JOIN (Cartesian product).

Example:


groupBy()

Group results by one or more columns.

Examples:


having()

Filter grouped results (use after groupBy()).

Examples:


orHaving()

Add an OR HAVING condition.

Example:


orderBy()

Sort results by one or more columns.

Parameters:

Examples:


limit()

Limit the number of results.

Parameters:

Examples:


offset()

Skip a number of records.

Example:


Query Execution Methods

These methods execute the query and return results.


get()

Execute the query and return all matching records.

Parameters:

Returns: Array of associative arrays

Examples:


all()

Get all records from the table (no filtering).

Example:

Note: Equivalent to DB::table('users')->get() without any conditions.


first()

Get the first matching record.

Returns: Associative array (single record) or empty array if no match

Examples:


find()

Find a record by its ID.

Parameters:

Returns: Single record or empty array

Examples:

Note: If your table primary key column is not id then this is not for you because it's a convention that you must be follows.


count()

Count the number of matching records.

Returns: Integer count

Examples:


exists()

Check the presence of the records.

Returns: Boolean presence

Examples:


insert()

Insert a new record into the table.

Parameters:

Returns: true on success

Examples:

Note: Does not return the inserted ID. Use DB::raw("SELECT LAST_INSERT_ID()") if needed.


update()

Update existing records.

Parameters:

Returns: true on success

Important: Requires at least one WHERE condition

Examples:


delete()

Delete records from the table.

Returns: true on success

Important: Requires at least one WHERE condition

Examples:


insertOrUpdate()

Insert a new record or update if it exists.

Parameters:

Returns: true on success

Examples:

Note: Executes a SELECT COUNT query first. For better performance with unique keys, consider using MySQL's INSERT ... ON DUPLICATE KEY UPDATE.


toRawSql()

Get the generated SQL query without executing it. (for debugging)

Returns: SQL query string with ? placeholders

Examples:

Use Case: Debugging, logging, or query optimization analysis.


Practical Examples

User Management System

E-commerce Product Search

Analytics Dashboard

Blog System


Security Best Practices

1. Always Use Prepared Statements

BuildQL automatically uses prepared statements for all values. Never concatenate user input:

2. Validate Column Names

While BuildQL validates column names, always validate user-controlled column names:

3. Sanitize User Input

Always validate and sanitize data before insertion:

4. Use Transactions for Critical Operations

For operations involving multiple tables, use transactions via raw queries:


Error Handling

BuildQL throws BuilderException for all database errors:

Exception Methods

The BuilderException class provides two parameters:

Common Exceptions

Connection Errors:

Query Errors:

Validation Errors:


Advanced Usage

Complex Queries

Dynamic Query Building

Pagination Helper (Example Implementation)

E.g; The following is an example helper function you can add to your project:

Query Logging (Example Implementation)


Performance Tips

1. Use Specific Columns

2. Add Indexes

Ensure frequently queried columns have database indexes:

3. Limit Results

Always use limit() for large tables:

4. Use first() Instead of get()[0]

5. Avoid N+1 Queries


Testing

BuildQL includes comprehensive Pest PHP tests. Run tests:

Writing Tests


Migration from Other Query Builders

From Laravel Eloquent

BuildQL syntax is very similar to Laravel:

Key Differences:

From PDO


Troubleshooting

Issue: "Connection is not established"

Solution: Call DB::setConnection() or DB::boot() before using queries.


Issue: "Connection already established"

Solution: You can only connect once. To reconnect:


Issue: "Where method is not optional in update case"

Solution: update() and delete() require WHERE conditions:


Issue: "Invalid column name"

Solution: Column names can only contain letters, numbers, underscores, dots, hyphens, and colons (for aliases) and parenthesis () if are using aggregates function :


Issue: Query returns empty array

Debugging:


Limitations

  1. No ORM Features: BuildQL is a query builder, not an ORM. No models or relationships.
  2. Single Database: Only supports MySQL/MariaDB via MySQLi.
  3. No Transactions API: Use DB::raw() for transaction control.
  4. No Query Caching: Results are not cached automatically.
  5. Basic Aggregates: Complex window functions not supported.
  6. AND/OR Precedence: Be aware of SQL operator precedence in complex WHERE clauses.

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Write tests for new features
  4. Ensure all tests pass: ./vendor/bin/pest
  5. Submit a pull request

Changelog

Version 1.0.1 (Initial Release)


License

MIT License - see LICENSE file for details.


Support


Credits

Author: Umar Ali

Inspired by: Laravel Query Builder

Built with ❤️ for the PHP community.


Quick Reference Card


Happy Querying with BuildQL!


All versions of query-builder with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
ext-mysqli Version *
vlucas/phpdotenv Version ^5.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package buildql/query-builder contains the following files

Loading the files please wait ...