PHP code example of kynetcode / wpzylos-database

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

    

kynetcode / wpzylos-database example snippets


use WPZylos\Framework\Database\DatabaseServiceProvider;

// In your plugin's boot configuration
'providers' => [
    DatabaseServiceProvider::class,
],

use WPZylos\Framework\Database\Connection;

// Resolve from container
$db = $app->make(Connection::class);
// or
$db = $app->make('db');

// Fluent queries via QueryBuilder
$users = $db->table('users')->get();
$user  = $db->table('users')->where('id', 1)->first();

// Get all records
$users = $db->table('users')->get();

// Get first record
$user = $db->table('users')->where('email', $email)->first();

// Select specific columns
$names = $db->table('users')->select('id', 'name')->get();

// With conditions
$active = $db->table('users')
    ->where('status', 'active')
    ->where('role', 'admin')
    ->get();

// Comparison operators
$expensive = $db->table('products')
    ->where('price', '>', 100)
    ->orderBy('price', 'DESC')
    ->limit(10)
    ->get();

// Where IN
$selected = $db->table('users')
    ->whereIn('id', [1, 2, 3])
    ->get();

// Count
$total = $db->table('users')
    ->where('status', 'active')
    ->count();

// Via QueryBuilder — returns insert ID or false
$id = $db->table('users')->insert([
    'name' => 'John Doe',
    'email' => '[email protected]',
]);

$affected = $db->table('users')
    ->where('id', 1)
    ->update(['status' => 'inactive']);

$affected = $db->table('users')
    ->where('id', 1)
    ->delete();

// Uses Connection::find() directly (', 1);

// Custom primary key column
$item = $db->find('wp_myplugin_items', 'abc-123', 'uuid');

// Uses Connection::insertGetId() directly ( [
    'name' => 'Jane Doe',
    'email' => '[email protected]',
]);

$orderId = $db->transaction(function () use ($db, $orderData, $paymentData) {
    $orderId = $db->table('orders')->insert($orderData);

    $db->table('payments')->insert([
        'order_id' => $orderId,
        ...$paymentData,
    ]);

    return $orderId;
});

$db->beginTransaction();

try {
    $db->table('orders')->insert($order);
    $db->table('inventory')
        ->where('product_id', $productId)
        ->update(['stock' => $newStock]);

    $db->commit();
} catch (\Throwable $e) {
    $db->rollback();
    throw $e;
}

// Execute raw SQL
$db->query("UPDATE `{$table}` SET views = views + 1 WHERE id = %d", $id);

// Get a single row
$user = $db->getRow("SELECT * FROM `{$table}` WHERE email = %s", $email);

// Get multiple rows
$logs = $db->getResults("SELECT * FROM `{$table}` WHERE level = %s", 'error');

// Get a single value
$count = $db->getVar("SELECT COUNT(*) FROM `{$table}` WHERE status = %s", 'active');

$db->table('users')->insert($data);

if ($db->hasError()) {
    $error = $db->lastError();  // Error message string
}

$affected = $db->rowsAffected();
$lastId = $db->lastInsertId();