PHP code example of ocolin / easydb

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

    

ocolin / easydb example snippets


// Setting up variables manually for visual purposes
$_ENV['PREFIX_DB_HOST'] = 'localhost';
$_ENV['PREFIX_DB_NAME'] = 'mydb';
$_ENV['PREFIX_DB_USER'] = 'admin';
$_ENV['PREFIX_DB_PASS'] = 'password1234';
$_ENV['PREFIX_DB_PORT'] = 3306 // This is the default so does not need to be set.

$prefix_db = Ocolin\EasyDB\DB::fromEnv( prefix: 'PREFIX' );

/*
 * Contents of .env file:
 * MYDATA_DB_HOST=localhost
 * MYDATA_DB_NAME=mydb
 * MYDATA_DB_USER=admin
 * MYDATA_DB_PASS=password1234
 */

$prefix_db = Ocolin\EasyDB\DB::fromEnv( 
    prefix: 'MYDATA', files: __DIR__ . '/../.env' 
);


$pdo_handler = Ocolin\EasyDB\DB::connect(
    host: 'localhost',
    name: 'mydb',
    user: 'admin',
    pass: 'password1234',
    port: 3306 // This is default and does not need to be set.
);

$output = Ocolin\EasyDB\Query::createColumns( 
    params: [ 'A' => '1', 'B' => '2', 'C' => '3'] 
); 
// output: `A`, `B`, `C`

$output = Ocolin\EasyDB\Query::createColumnValues( 
    params: [ 'A' => '1', 'B' => '2', 'C' => '3']
);
// output: ":A, :B, :C"

$output = Ocolin\EasyDB\Query::replaceInto( 
    table: 'mytable', params: [ 'A' => '1', 'B' => '2', 'C' => '3' ] 
);
/*
    REPLACE INTO mytable
        ( `A`, `B`, `C` )
    VALUES
        ( :A, :B, :C )
 */

Ocolin\EasyDB\Query::bindParameters( 
    query: $pdo_statement, params: [ 'A' => '1', 'B' => '2', 'C' => '3' ] 
);

Ocolin\EasyDB\Query::bindValues( 
    query: $pdo_statement, params: [ 'A' => '1', 'B' => '2', 'C' => '3' ] 
);

$newparams = Ocolin\EasyDB\Query::filterColumns(
    params: [ 'A' => '1', 'B' => '2', 'C' => '3' ],
    allowed: [ 'A', 'B', 'D' ]
)

// $newparams = [ 'A' => '1', 'B' => '2' ]

$output = Ocolin\EasyDB\Query::replaceInto( 
    table: 'mytable', params: $newparams
);

$output = Ocolin\EasyDB\Validate::isCHAR( input: ['A'] );
// "CHAR: Input must be a string, got array."

$output = Ocolin\EasyDB\Validate::isCHAR( input: 'A' );
// TRUE