PHP code example of migliori / php-pdo-database-class

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

    

migliori / php-pdo-database-class example snippets


    PDO_DRIVER // 'mysql', 'firebird', 'oci' or 'pgsql'
    DB_HOST    // For instance 'localhost'
    DB_NAME    // Your database name
    DB_USER    // Your database username
    DB_PASS    // Your database password
    DB_PORT[OPTIONAL]    // The default port is 3306
    

    use Migliori\Database\Db;

    // register the database connection settings
    w all the encountered errors automatically
    $db = new DB(true);

    // or connect, then test the connection and retrieve the error if the database is not connected
    $db = new DB();
    if (!$db->isConnected()) {
        $error_message = $db->error();
    }
    

    // Select rows without using SQL
    $values = array('id', 'first_name', 'last_name');
    $where = array('country' => 'Indonesia');
    $db->select('customers', $values, $where);

    // We can make more complex where clauses in the Select, Update, and Delete methods
    $values = array('id', 'first_name', 'last_name');
    $where = array(
        'zip_code IS NOT NULL',
        'id >' => 10,
        'last_name LIKE' => '%Ge%'
    );
    $db->select('customers', $values, $where);

    // Let's sort by descending ID and run it in debug mode
    $extras = array('order_by' => 'id DESC');
    $db->select('customers', $values, $where, $extras, true);


    // loop through the results
    while ($row = $db->fetch()) {
        echo $row->first_name . ' ' . $row->last_name . '<br>';
    }

    // or fetch all the records then loop
    // (this function should not be used if a huge number of rows have been selected, otherwise it will consume a lot of memory)
    $rows = $db->fetchAll();

    foreach ($rows as $row) {
        echo $row->first_name . ' ' . $row->last_name . '<br>';
    }
    

    PDO_DRIVER // 'mysql', 'firebird', 'oci' or 'pgsql'
    DB_HOST    // For instance 'localhost'
    DB_NAME    // Your database name
    DB_USER    // Your database username
    DB_PASS    // Your database password
    DB_PORT[OPTIONAL]    // The default port is 3306
    

    use Migliori\Database\Pagination;
    use Migliori\Database\PdoSelectParams;

    // register the database connection settings
        $where = array('country' => 'Indonesia');

    $pdo_select_params = new PdoSelectParams('customers', $values, $where);

    // create the Pagination object
    $db = new Pagination($pdo_select_params);

    // get the records and the pagination HTML code
    $pagination_html = $db->pagine();

    // count the records and display them
    $records_count = $db->rowCount();

    if (!empty($records_count)) {
        while ($row = $db->fetch()) {
            echo $row->first_name . ' ' . $row->last_name . ' : ' . $row->country . '<br>';
        }
    }

    echo $pagination_html;

    
bash
php ./vendor/bin/phpunit test