PHP code example of zi / zquery

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

    

zi / zquery example snippets


use ZQuery\ZQuery;

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$zq = new ZQuery(new mysqli('127.0.0.1', 'user', 'pass', 'app'));

$users = $zq->table('users')
    ->select(['id', 'email'])
    ->where('status', '=', 'active')
    ->latest('created_at')
    ->limit(10)
    ->get();
// [['id' => 42, 'email' => '[email protected]'], ...]

$pdo = new PDO('mysql:host=127.0.0.1;dbname=app', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$zq = new ZQuery($pdo);

$user   = $zq->table('users')->where('email', '=', '[email protected]')->first();   // row or null
$count  = $zq->table('users')->where('status', '=', 'active')->count();         // int
$emails = $zq->table('users')->where('status', '=', 'active')->pluck('email');  // list
$id     = $zq->table('users')->insert(['email' => '[email protected]'])->insertGetId();
$zq->table('products')->upsert(['sku' => 'A1', 'qty' => 5], 'sku')->executeUpsert();   // insert or update

$zq->transaction(function (ZQuery $db) {
    $db->table('users')->where('id', '=', 10)->update(['status' => 'disabled'])->executeUpdate();
    $db->statement('INSERT INTO audit_logs (action, user_id) VALUES (?, ?)', ['user.disabled', 10]);
});