scrnr / query-builder
QueryBuilder for MySQL driver
Requires
- php: >=8.0
- ext-pcre: *
README
For full documentation click here
Table of contents
Description 🔝
The PHP Database Query Library is a powerful and easy-to-use PHP library that allows you to easily write queries to database and receive a string with the builded query. It provides a simple and intuitive interface for building complex SQL statements in your PHP code.
Features 🔝
- Easy to use: The library provides a straightforward interface for building queries. You can easily create complex SQL statements without having to write raw SQL code.
- Secure: The library is designed with security in mind. It uses prepared statements to prevent SQL injection attacks.
- Flexible: The library supports a wide range of SQL operations such as SELECT, INSERT, UPDATE, DELETE.
- Object-oriented approach: The library follows an object-oriented approach to make it easier for developers to work with databases in PHP.
- Easy to integrate: The library is easy to integrate with existing PHP projects.
Installation 🔝
You can install this library using Composer. Just add the following line to your composer.json
file:
"require": { "scrnr/query-builder": "*" }
Or you can use this command:
composer require scrnr/query-builder
How to use 🔝
To use the library, first, you need to create a new instance of the QueryBuilder
class:
use Scrnr\QueryBuilder\QueryBuilder; $queryBuilder = new QueryBuilder();
Then, you can use the various methods provided by the class to build your query.
The QueryBuilder
class has four public methods:
Example 🔝
The following example demonstrates how to use the QueryBuilder
class to build SQL statements.
<?php // Include the namespace use Scrnr\QueryBuilder\QueryBuilder; // Require the Composer autoload file require_once __DIR__ . '/vendor/autoload.php'; // Create a new QueryBuilder instance $queryBuilder = new QueryBuilder();
First example 🔝
$queryBuilder->select('products')->all()->getQuery();
Output
SELECT products.* FROM products
In this example, we call the select()
method with the table name and the all()
method to select all columns. Then, we call the getQuery()
method to get the SQL query.
Second example 🔝
$queryBuilder->select('posts') ->columns('id', 'title', 'content', 'date') ->from('category')->alias('title', 'category') ->innerJoin('categories')->on('category_id', 'id') ->getQuery();
Output
SELECT posts.id, posts.title, posts.content, posts.date, category.title AS category FROM posts INNER JOIN categories ON posts.category_id = categories.id
In this example, we call the select()
method with the table name, the columns()
method to select specific columns, the from()
method with the second table name, and the alias()
method to specify the column and its alias, and the innerJoin()
method to join the categories table. Finally, we call the getQuery()
method to get the SQL query
Third example 🔝
$queryBuilder->select('products') ->all() ->where() ->equal('category') ->end() ->order() ->limit() ->getAll(['laptop', 'price', 10]);
Output
Array ( [0] => SELECT products.* FROM products WHERE products.category = :products_category ORDER BY :order ASC LIMIT :limit [1] => Array ( [:products_category] => laptop [:order] => price [:limit] => 10 ) )
In this example, we call the select()
method with the table name and the all()
method to select all columns. Then, we call the where()
method to start building the WHERE clause, the equal()
method to specify the condition, and the end()
method to end the WHERE clause. Next, we call the order()
method to order the results and the limit()
method to limit the number of results returned. Finally, we call the getAll()
method to get the SQL query and its prepared parameters as an array.
For more information about the getAll()
method click here.
Contribution 🔝
Contributions to this library are welcome. You can report issues, suggest new features or submit pull requests on the GitHub repository.
Author 🔝
👤 GitHub: scrnr
License 🔝
This library is released under the MIT License. Please review the LICENSE file for more information