PHP code example of ideaglory / database

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

    

ideaglory / database example snippets


use Ideaglory\Database;

$db = Database::getInstance('host', 'username', 'password', 'database', 'charset');

$sql = "INSERT INTO users (name, email) VALUES (?, ?)";
$params = ['John Doe', '[email protected]'];
$db->query($sql, $params);

$sql = "SELECT * FROM users WHERE status = ?";
$params = ['active'];
$users = $db->fetchAll($sql, $params);
print_r($users);

$sql = "SELECT * FROM users WHERE id = ?";
$params = [1];
$user = $db->fetchOne($sql, $params);
print_r($user);

$db->beginTransaction();

$db->commit();

$db->rollback();

$db->close();

use Ideaglory\Database;

try {
    $db = Database::getInstance('localhost', 'root', '', 'example_db', 'utf8mb4');

    // Insert a new user
    $db->query("INSERT INTO users (name, email) VALUES (?, ?)", ['Alice', '[email protected]']);

    // Fetch all active users
    $users = $db->fetchAll("SELECT * FROM users WHERE status = ?", ['active']);
    print_r($users);

    // Start a transaction
    $db->beginTransaction();

    // Update a user
    $db->query("UPDATE users SET email = ? WHERE id = ?", ['[email protected]', 1]);

    // Commit the transaction
    $db->commit();

} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
    $db->rollback(); // Rollback if an error occurs
} finally {
    $db->close(); // Close the connection
}