PHP code example of tasoft / php-pdo

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

    

tasoft / php-pdo example snippets


$gen = $PDO->select("SELECT * FROM XXX");
// Now $gen is a generator, that means nothing happens until you want to fetch the records.

// ... more code

// Now, fetch the SQL records:
foreach($gen as $record) {
    // ... 
}

$gen = $PDO->select("SELECT * FROM XXX WHERE id = ?", [$objectID]);
// For secure SQL request.

$gen = $PDO->inject("INSERT INTO XXX (name, email) VALUES (?, ?)");
// Now again nothing happend yet.

// To insert records, just use.

$gen->send(["Thomas", "[email protected]"]);
// As many times you want!

$PDO->setTypeMapper( new DateMapper() );
// Now the methods selectWithObjects and injectWithObjects will convert raw values into their object values.

$record = $PDO->selectOneWithObjects("SELECT * FROM XXX");
print_r($record);
/*
Might look like:
Array (
    'the_date' => TASoft\Util\ValueObject\Date ... ,
    'the_date_time' => TASoft\Util\ValueObject\DateTime ... ,
    'the_time' => TASoft\Util\ValueObject\Time ...
)

// SQL:
CREATE TABLE XXX (
    the_date DATE DEFAULT NULL,
    the_date_time DATETIME DEFAULT NULL,
    the_time TIME DEFAULT NULL
)
*/

// This also works backward:
$newDate = new TASoft\Util\ValueObject\Date("1999-05-23");
$PDO->injectWithObjcts("UPDATE XXX SET the_date = ? WHERE ...")->send([$newDate, ...]);

// Using MapperChain allows to combine more than one type mapper.

$PDO->transaction(function() {
    /** @var TASoft\Util\PDO $this */
    $this->inject(....);
    
    $this->exec( .... );
    ...
});