PHPackages                             montyhusor/pqbuilder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. montyhusor/pqbuilder

ActiveLibrary[Database &amp; ORM](/categories/database)

montyhusor/pqbuilder
====================

A simple PHP query builder for MySQL, aimed at simplifying database interactions in web development

v0.5.2(2y ago)012MITPHPPHP &gt;=8.2.4

Since Mar 31Pushed 2y ago1 watchersCompare

[ Source](https://github.com/montyhusor/PQbuilder)[ Packagist](https://packagist.org/packages/montyhusor/pqbuilder)[ RSS](/packages/montyhusor-pqbuilder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

PQbuilder
=========

[](#pqbuilder)

> PQbuilder 是一个为MySQL设计的PHP查询构建库，使开发者能够以链式调用的方式构建SQL查询。 PQbuilder is a PHP query builder library designed for MySQL, enabling developers to construct the SQL queries through chainable method calls.

主要特性
----

[](#主要特性)

- **基础CRUD操作 - Basic CRUD Operations**：支持SELECT、INSERT、UPDATE、DELETE查询。- Supports SELECT, INSERT, UPDATE and DELETE queries.
- **链式调用 - Chainable Calls**：支持链式调用。- Supports chainable calls.
- **自动参数绑定 - Automatic Parameter Binding**：减少SQL注入的风险。- Reduces the risk of SQL injection.
- **条件构建 - Condition Building**：支持`IN`语句、嵌套逻辑和复杂条件构建。- Supports `IN` statements, nested logic, and the construction of complex conditions.
- **事务处理 - Transaction Management**：有事务开始、提交和回滚的方法。- Includes methods for starting transactions, committing, and rolling back.
- **数据抓取方法 - Data Fetching Methods**：提供`qfetchAll`、`qfetchF`、`qfetchColumn`、`qfetchObject`方法。- Provides `qfetchAll`, `qfetchF`, `qfetchColumn` and `qfetchObject`. methods.

安装 - Installation
-----------------

[](#安装---installation)

通过Composer进行安装：

```
composer require montyhusor/pqbuilder
```

示例 - Examples
-------------

[](#示例---examples)

### 插入 - INSERT

[](#插入---insert)

```
use PQbuilder\Factory as op;
use PQbuilder\Executor;

$config = [
    "host" => "host",
    "db" => "database",
    "user" => "username",
    "pass" => "password",
    "options" => [],
];

$eor = new Executor($config);

$insert = op::qinsert("testTable_library")
    ->columns(["userID", "borrower", "bookname"])
    ->values(["10001", "A1", "phpMyAdmin Starter"])
    ->values(["10002", "A2", "PHP is Standing Tall"]);

$eor->qexecute($insert);
echo "Affected rows: " . $eor->qrowCount();
```

### 更新 - UPDATE

[](#更新---update)

```
use PQbuilder\Factory as op;
use PQbuilder\Executor;

$eor = new Executor($config);

$update = op::qupdate("testTable_library")
    ->set("bookname", "PHP Cookbook")
    ->where("borrower", "=", "A1");

$eor->qexecute($update);
echo "Affected rows: " . $eor->qrowCount();
```

### 删除 - DELETE

[](#删除---delete)

```
use PQbuilder\Factory as op;
use PQbuilder\Executor;

$eor = new Executor($config);

$delete = op::qdelete("testTable_library")
    ->where("bookname", "=", "PHP Cookbook");

$eor->qexecute($delete);
echo "Affected rows: " . $eor->qrowCount();
```

### 查询 - SELECT

[](#查询---select)

```
use PQbuilder\Factory as op;
use PQbuilder\Executor;

$eor = new Executor($config);

$select = op::qselect(["id", "userID", "borrower", "bookname"], "testTable_library")
    ->where("userID", "IN", [10001, 10002, 10003]);

// 获取所有记录
$records = $eor->qfetchAll($select);

// 获取第一条记录
$firstRecord = $eor->qfetchF($select);

// 获取单列的第一个值
$ColumnfirstV = $eor->qfetchColumn($select, 3);
```

### 使用`pa()`和`endPa()`进行嵌套逻辑的查询 - Using `pa()` and `endPa()` for Nested Logic Queries

[](#使用pa和endpa进行嵌套逻辑的查询---using-pa-and-endpa-for-nested-logic-queries)

```
use PQbuilder\Factory as op;
use PQbuilder\Executor;

$eor = new Executor($config);

$select = op::qselect(["id", "userID", "borrower", "bookname"], "testTable_library")
    ->where("userID", "=", 10002)
    ->pa()
        ->where("bookname", "=", "PHP Cookbook")
        ->orWhere("bookname", "=", "PHP is Standing Tall")
    ->endPa();

$records = $eor->qfetchAll($select);
```

### 使用`qfetchObject`获取记录作为对象 - Using qfetchObject to Fetch Records as Objects

[](#使用qfetchobject获取记录作为对象---using-qfetchobject-to-fetch-records-as-objects)

定义Book类：

```
class Book
{
    public $bookname;
    public $greeting;

    public function __construct($greeting = "Hi")
    {
        $this->greeting = $greeting;
    }

    public function intro()
    {
        return $this->greeting . " This book is called 《" . $this->bookname . "》";
    }
}
```

获取Book对象：

```
use PQbuilder\Factory as op;
use PQbuilder\Executor;

$eor = new Executor($config);

$selectBook = op::qselect(["bookname"], "testTable_library");
$bookObject = $eor->qfetchObject($selectBook, Book::class, ["Welcome to the library!"]);

echo $bookObject->intro();
```

### 使用`groupBy()`, `orderBy()`, `having()`, `limit()` - Using `groupBy()`, `orderBy()`, `having()`, `limit()`

[](#使用groupby-orderby-having-limit---using-groupby-orderby-having-limit)

```
use PQbuilder\Factory as op;
use PQbuilder\Executor;

$eor = new Executor($config);

$selectX = op::qselect(
        ["bookname", "COUNT(id) AS count"],
        "testTable_library"
    )
        ->groupBy(["bookname"])
        ->having("COUNT(id)", ">", 1)
        ->orderBy("count", "DESC")
        ->limit(3);

$records = $eor->qfetchAll($selectX);
echo "";
print_r($records);
```

注意事项 - Considerations
---------------------

[](#注意事项---considerations)

- 在构建DELETE和UPDATE查询时，请确保不在没有明确条件的情况下直接使用`pa()`和`endPa()`，以防止执行无条件的全表操作并导致数据丢失。
- 当开始一个新的逻辑组合时，使用的第一个`where()`和`orWhere()`方法都会产生相同的效果。因此，在开始新的逻辑组合时直接使用where就行。
- 类似地，对于`pa()`方法，当你开始一个新的条件组合时，无论是`pa()`还是`pa("OR")`，如果它是第一个使用的，那么效果是相同的，因为它标志着一个新的逻辑分支的开始。
- 请注意，虽然`PQbuilder`旨在简化SQL查询构建过程，并提供了基础的CRUD操作及一些高级功能，但它并不覆盖所有的SQL查询操作以及一些更复杂的数据库操作，`PQbuilder`还有待完善。

> - When building DELETE and UPDATE queries, please ensure not to directly use `pa()` and `endPa()` without specific conditions, to prevent executing unconditional full-table operations and causing data loss.
> - When initiating a new logical grouping, the first use of `where()` and `orWhere()` methods will produce the same effect. Therefore, it is sufficient to directly use `where` when starting a new logical combination.
> - Please note that while `PQbuilder` is designed to simplify the SQL query building process, offering basic CRUD operations and some advanced features, it does not cover all SQL query operations or some of the more complex database interactions. `PQbuilder` is still a work in progress.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

3

Last Release

759d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e4d83dee8851205ac797043ab5d4b397b7009e507f7c136663f6e623828b4470?d=identicon)[montyhusor](/maintainers/montyhusor)

---

Top Contributors

[![montyhusor](https://avatars.githubusercontent.com/u/165127329?v=4)](https://github.com/montyhusor "montyhusor (4 commits)")

### Embed Badge

![Health badge](/badges/montyhusor-pqbuilder/health.svg)

```
[![Health](https://phpackages.com/badges/montyhusor-pqbuilder/health.svg)](https://phpackages.com/packages/montyhusor-pqbuilder)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M542](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
