PHP code example of llwebsol / easy-db

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

    

llwebsol / easy-db example snippets


$query = 'SELECT * FROM users WHERE id = :user_id';
$params = [':user_id' => 7]

$user = $db->queryOne($query,$params);

$data = [
    'email' => '[email protected]'
];

$rows_affected = $db->update('users', 76, $data);

use EasyDb\Events\Listener;

class QueryListener implements Listener
{
    /**
     * @param EventData $data
     * @param array     &$ref_parameters [optional]
     */
    public static function handleEvent(EventData $data, array &$ref_parameters = []){
        echo $data->getSql();
    }
}

// Register the listener
Listeners::register(Event::BEFORE_QUERY, QueryListener::class);

use EasyDb\Events\Listener;

class InsertListener implements Listener
{
    /**
     * @param EventData $data
     * @param array     &$ref_parameters [optional]
     */
    public static function handleEvent(EventData $data, array &$ref_parameters = []){
        $ref_parameters['created_user'] = $_SESSION['user'];
    }
}

// Register the listener
Listeners::register(Event::BEFORE_QUERY, InsertListener::class);

/**
* @param array $save_records
* @param DB    $db
*
* @return int $records_saved
*/
function save_a_bunch_of_records(array $save_records, DB $db){
    $db->beginTransaction();

    $records_saved = 0;
    foreach($save_records as $table_name => $record){
        try{
            $records_saved += $db->save($table_name, $record);
        }
        catch(QueryException $ex){

            // All or nothing. Undo all previous saves
            $db->rollbackTransaction();

            return false;
        }
    }

    $db->commitTransaction();
    return $records_saved;
}