PHP code example of alexsasharegan / database

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

    

alexsasharegan / database example snippets







// library defaults
$connectionOptions = [
    'DB_HOST'     => '127.0.0.1',
    'DB_NAME'     => 'test',
    'DB_PORT'     => '3306',
    'DB_CHARSET'  => 'utf8',
    'DB_USERNAME' => 'admin',
    'DB_PASSWORD' => 'admin',
];

// library defaults
$pdoOptions = [
	PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
	PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
	PDO::ATTR_EMULATE_PREPARES   => FALSE,
	PDO::ATTR_STRINGIFY_FETCHES  => FALSE,
];

$mySQL = new \Database\MySQL($connectionOptions, $pdoOptions);



use Database\MySQL;

# Takes a MySQL-formatted date string and returns a string file path
MySQL::SQLDateToPath( string $SQLDate );
# example
echo MySQL::SQLDateToPath( '2016-09-06 14:02:26' );
# Outputs: '2016/09/06'

# Returns a MySQL-formatted timestamp
echo MySQL::now();
# Outputs: '2016-09-06 14:04:15';



$mySQL = new \Database\MySQL($connectionOptions, $pdoOptions);

# use a try/catch block to handle a bad query
try {
  $mySQL->select(['firstName', 'lastName'])
        ->from('users')
        ->where('id', 'in', [1,2,3])
        # we can chain methods together here
        ->map(
        # this can be any callable type ( will be called with each row )
        # closures let us 'use' vars from parent scope
        # be wary of when you need to pass by reference using &
        function ( array $resultRow ) 
        {
            $users[] = new User($resultRow); # the $resultRow is an associative array
        }
    );
} catch ( Exception $e ) {
  # insert some custom error handling here
  exit( $e );
}