PHP code example of pjanisio / simpledb

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

    

pjanisio / simpledb example snippets


// Example: Using the execute method to run a raw SQL statement
    $sql = "UPDATE `users` SET `email` = '[email protected]' WHERE `username` = 'john_doe'";
    $stmt = $db->execute($sql);

    echo "Number of affected rows: " . $db->rowCount($stmt) . "\n";




pleDB\SimpleDB;

try {
    // Create a new instance of the SimpleDB class
    $db = new SimpleDB(dbName: 'my_database', username: 'username', password: 'password');

    // Check if the connection is successful
    if ($db->isConnected()) {
        echo "Connected to the database successfully.\n";
    } else {
        echo "Failed to connect to the database.\n";
    }

    // Example: Executing a query to fetch all rows from a table
    $sql = 'SELECT * FROM `users`';
    $result = $db->fetchAll($sql);

    echo "Data from users table:\n";
    foreach ($result as $row) {
        print_r($row);
    }

    // Example: Inserting a new record into the table
    $insertData = [
        'username' => 'john_doe',
        'email' => '[email protected]',
    ];

    if ($db->insert('users', $insertData)) {
        echo "Record inserted successfully.\n";
    } else {
        echo "Failed to insert record.\n";
    }

    // Example: Updating a record in the table
    $updateData = [
        'email' => '[email protected]',
    ];
    $where = "username = 'john_doe'";

    if ($db->update('users', $updateData, $where)) {
        echo "Record updated successfully.\n";
    } else {
        echo "Failed to update record.\n";
    }

    // Example: Deleting a record from the table
    $where = "username = 'john_doe'";

    if ($db->delete('users', $where)) {
        echo "Record deleted successfully.\n";
    } else {
        echo "Failed to delete record.\n";
    }

    // Example: Truncating the users table
    if ($db->truncate('users')) {
        echo "Table truncated successfully.\n";
    } else {
        echo "Failed to truncate table.\n";
    }

} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}