PHP code example of r7di4am / rdb

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

    

r7di4am / rdb example snippets




tabase connection
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Create an instance of the rdb class
use rdb\rdb;
$db = new rdb($pdo);

// Run a query to select all users
$db->query("SELECT * FROM users");

// Fetch all results
$users = $db->fetch_all();

// Print the results
echo "<pre>" . json_encode($users, JSON_PRETTY_PRINT) . "</pre>";

$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$db = new rdb($pdo);
$db->query("SELECT * FROM users");
$users = $db->fetch_all();
echo "<pre>" . json_encode($users, JSON_PRETTY_PRINT) . "</pre>";


$db->query("SELECT * FROM users WHERE id = :id", ['id' => 2]);
$user = $db->fetch_assoc();
echo "<pre>" . json_encode($user, JSON_PRETTY_PRINT) . "</pre>";


$sql = "INSERT INTO users (username, password) VALUES (:username, :password)";
$params = ['username' => 'r7di4am', 'password' => '12345678'];

$db->query($sql, $params);
echo "User inserted successfully.";

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

    // Insert a new user
    $sql = "INSERT INTO users (id, username, password) VALUES (NULL, :username, :password)";
    $params = ['username' => 'r7di4am', 'password' => 'somthing'];
    $db->query($sql, $params);

    // Delete a user
    $sql = "DELETE FROM users WHERE id = :id";
    $db->query($sql, ["id" => $users[0]['id']]);

    // Commit the transaction (save the changes)
    $db->commit();
    echo "<h3>Users inserted successfully and one user deleted.</h3>";
} catch (Exception $e) {
    // If something goes wrong, cancel the changes
    $db->rollback();
    echo "<h3>Error:</h3> " . $e->getMessage();
}