PHP code example of shivanraptor / php-db-manager

1. Go to this page and download the library: Download shivanraptor/php-db-manager 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/ */

    

shivanraptor / php-db-manager example snippets




use PhpDbManager\DbManager;

try {
    $db = new DbManager([
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'your_password',
        'database' => 'your_database',
        'charset' => 'utf8mb4',
        'port' => 3306,
        'persistent' => false,
        'autocommit' => true,
        'retry_attempts' => 3,
        'retry_delay' => 100 // milliseconds
    ]);

    // Execute a prepared statement
    $result = $db->execute(
        "SELECT * FROM users WHERE id = ? AND status = ?",
        ['i' => 1, 's' => 'active']
    );

    // Fetch a single row
    $user = $db->fetch($result);

    // Or fetch all rows
    $users = $db->fetchAll($result);

} catch (Exception $e) {
    error_log('Database error: ' . $e->getMessage());
    // Handle error appropriately
}

// Select query
$result = $db->execute("SELECT * FROM users WHERE id = ?", ['i' => 1]);
$user = $db->fetch($result);

// Insert query
$db->execute(
    "INSERT INTO users (name, email) VALUES (?, ?)",
    ['s' => 'John Doe', 's' => '[email protected]']
);
$userId = $db->lastInsertId();

// Update query
$db->execute(
    "UPDATE users SET status = ? WHERE id = ?",
    ['s' => 'active', 'i' => 1]
);
$affectedRows = $db->affectedRows();

try {
    $db->beginTransaction();
    
    $db->execute(
        "INSERT INTO orders (user_id, total) VALUES (?, ?)",
        ['i' => 1, 'd' => 99.99]
    );
    
    $db->execute(
        "UPDATE inventory SET stock = stock - 1 WHERE product_id = ?",
        ['i' => 123]
    );
    
    $db->commit();
} catch (Exception $e) {
    $db->rollback();
    throw $e;
}

// Fetch as associative array
$result = $db->execute("SELECT * FROM users");
$users = $db->fetchAll($result, 'assoc');

// Fetch as object
$result = $db->execute("SELECT * FROM users");
$users = $db->fetchAll($result, 'object');

// Fetch as indexed array
$result = $db->execute("SELECT * FROM users");
$users = $db->fetchAll($result, 'array');

$info = $db->getConnectionInfo();
echo "Connected to {$info['server']} as {$info['user']}";
echo "MySQL version: {$info['version']}";
echo "Charset: {$info['charset']}";

// Old version
$db = new dbManager($host, $user, $pass, $dbname);

// New version
$db = new DbManager([
    'host' => $host,
    'username' => $user,
    'password' => $pass,
    'database' => $dbname
]);

use PhpDbManager\DbManager;

// Old version
if ($db->error !== NULL) {
    // error exists
}

// New version
try {
    $db = new DbManager($options);
} catch (Exception $e) {
    // Handle error
}

// Old version
$result = $db->query_prepare($sql, $params);
$row = $db->result($result);

// New version
$result = $db->execute($sql, $params);
$row = $db->fetch($result);
bash
composer