PHP code example of montyhusor / pqbuilder

1. Go to this page and download the library: Download montyhusor/pqbuilder 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/ */

    

montyhusor / pqbuilder example snippets


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();

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();

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();

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);

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);

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 . "》";
    }
}

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();

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 "<pre>";
print_r($records);