PHP code example of gorgo / golibdatabase

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

    

gorgo / golibdatabase example snippets


use golibdatabase\Database\MySql;
$connect = new MySql\ConnectInfo( 'username','password','hostname','default_shema' );
$db = new MySql( $connect );
$result = $db->select( 'SELECT * FROM Tablename' );
if ($result->getErrorNr()) {
        echo " --- mysql error:" . $result->getError();
    } else {
        echo " --- got " . $result->count() . 'entries ';
        var_dump( $result->getResult() );
    }
}

// run the whole code 3 times just to explain what the connection-manager is doing
for ($i = 0; $i < 3; $i++) {
    $connect = new MySql\ConnectInfo( 'username','password','hostname','default_shema' );

    $connectManager = new Database\ConnectManager();

    if ($connectManager->connectionIsStored( $connect )) {
        $db = $connectManager->getStoredConnection( $connect );
        echo ' --- use existing connection --- ';
    } else {
        echo ' ---- create a new connection --- ';
        $db = new MySql( $connect );
        $connectManager->registerConnection( $db );
    }

    $result = $db->select( 'SELECT * FROM Tablename' );

    if ($result->getErrorNr()) {
        echo " --- mysql error:" . $result->getError();

    } else {
        echo " --- got " . $result->count() . 'entries ';
        var_dump( $result->getResult() );

    }
}


/**
 * the property Class descriptes the expected fields
 */
use golib\Types;
/**
 * the property Class descriptes the expected fields
 */
class exampleProp extends Types\PropsFactory {
    /**
     * autoinc, primary
     * @var int
     */
    public $primId = NULL;
    /**
     *
     * @var string
     */
    public $Content = '';

    /**
     * a date example
     * @var Types\Timer
     */
    public $DateExample = Types\MapConst::TIMER;
    /**
     * just a integer
     * @var int
     */
    public $ExampleValue = 0;

}


use golib\Types;
/**
 * the table class
 * that maps to the table in the database.
 *
 * they need to know about the structure by using
 * the property class
 *
 * and (of course) the table name
 */
class exampleTable extends MySql\Table {
    /**
     * defines the content.
     * how the rows looks like
     * @return \exampleProp
     */
    public function getPropFactory () {
        return new exampleProp( 'primId' );
    }
    /**
     * just the tablename
     * @return string
     */
    public function getTableName () {
        return 'golib-db';
    }

}


// initiate the modell of the table
$golibTable = new exampleTable();

// get the content by using a existing database connection
$golibTable->fetchData( $db );

// one way to iterate the content.
$golibTable->foreachCall( function(exampleProp $row) {
    var_dump( $row );
} );

$where = new MySql\WhereSet();
$where->isEqual( 'ExampleValue', 9887 );

$golibTable = new exampleTable( $where );

$Limit = new MySql\Limit();
$Limit->count = 1;

$golibTable = new exampleTable( NULL, $limit );

$Limit = new MySql\Limit();

$Limit->count = 1;
$Limit->start = 1; // equal to LIMIT 1,1

$Limit->count = 100;
$Limit->start = 0; // equal to LIMIT 0,100