PHP code example of minphp / record

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

    

minphp / record example snippets


use Minphp\Record\Record;

$dbInfo = array(
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'databasename',
    'user' => 'user',
    'pass' => 'pass'
);

$record = new Record($dbInfo);

$users = $record->select()
    ->from('users')
    ->fetchAll();

$users = $record->select(array('id', 'name', 'email'))
    ->from('users')
    ->fetchAll();

$users = $record->select(array('id', 'name', 'email' => 'login'))
    ->from('users')
    ->fetchAll();

$users = $record->select(array('id', 'name', 'email' => 'login'))
    ->select(array('\'active\'' => 'status'), false)
    ->from('users')
    ->fetchAll();

$users = $record->select(array('MAX(id)' => 'largestId'))
    ->from('users')
    ->fetch();

$count = $record->select()
    ->from('users')
    ->numResults();

$count = $record->affectedRows();

$id = $record->lastInsertId();

$users = $record->select()
    ->from('users')
    ->limit(10)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->limit(10, 20)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->order(array('id' => 'asc'))
    ->fetchAll();

$users = $record->select(array('email'))
    ->from('users')
    ->group(array('email'))
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->where('id', '=', 10)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->where('id', '=', 10)
    ->where('name', '=', 'Roger Sherman')
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->where('id', '=', 10)
    ->orWhere('name', '=', 'Roger Sherman')
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->where('id', 'in', array(1, 2, 3, 4))
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->like('name', 'Roger%')
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->like('name', 'Roger%')
    ->like('email', '@domain.com')
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->like('name', 'Roger%')
    ->orLike('email', '@domain.com')
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->having('name', '!=', null)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->having('name', '!=', null)
    ->having('email', '!=', null)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->having('name', '!=', null)
    ->orHaving('email', '!=', null)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->open()
        ->where('id', '>', 123)
        ->orWhere('email', '!=', null)
    ->close()
    ->where('name', '!=', null);
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->innerJoin('user_groups', 'user_groups.id', '=', 'users.user_group_id', false)
    ->fetchAll();

    ->innerJoin('user_groups', 'user_groups.id', '=', 5)

$users = $record->select()
    ->from('users')
    ->leftJoin('user_groups', 'user_groups.id', '=', 'users.user_group_id', false)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->rightJoin('user_groups', 'user_groups.id', '=', 'users.user_group_id', false)
    ->fetchAll();

$users = $record->select()
    ->from('users')
    ->join('user_groups')
    ->fetchAll();

$usersQuery = $record->select()
    ->from('users')
    ->where('id', '=', 1234)->get();
$usersValues = $record->values;

$record->reset();

$groups = $record->select()
    ->from('user_groups')
    ->appendValues($usersValues)
    ->innerJoin(array($usersQuery => 'temp'), 'temp.user_group_id', '=', 'user_groups.id', false)
    ->fetchAll();

/*
SELECT * FROM user_groups
INNER JOIN (
    SELECT * FROM users
    WHERE id=1234
) AS temp ON temp.user_group_id=user_groups.id
*/

$record->insert('users', array('name' => 'Roger Sherman'));

$record->insert(
    'users',
    array('name' => 'Roger Sherman', 'bad_field' => 'will not be inserted'),
    array('name')
);

$record->duplicate('name' => 'Roger Sherman')
    ->insert(
        'users',
        array('id' => 1776, 'name' => 'Roger Sherman')
    );

$users = $record->select(array('id'))
    ->from('users');

$record->reset();
$record->insert('some_table', array('id' => $users));

$record->where('id', '=', 1776)
    ->update('users', array('name' => 'Roger Sherman'));

$record->where('id', '=', 1776)
    ->update(
        'users',
        array('name' => 'Roger Sherman', 'bad_field' => 'will not be updated'),
        array('name')
    );

$record->from('users')
    ->delete();

$record->from('users')
    ->innerJoin('user_groups', 'user_groups.id', '=', 'users.user_group_id', false)
    ->where('user_groups.id', '=', 1)
    ->delete(array('users.*', 'user_groups.*'));

/**
* Optionally set the character set and collation of the table being created
* $record->setCharacterSet('utf8mb4');
* $record->setCollation('utf8mb4_unicode_ci');
*/
$record->setField(
        'id',
        array('type' => 'int', 'size' => 10, 'unsigned' => true, 'auto_increment' => true)
    )
    ->setField('name', array('type' => 'varchar', 'size' => '128'))
    ->setField('emai', 'array('type' => 'varchar', 'size' => '255'))
    ->setKey(array('id'), 'primary')
    ->setKey(array('name'), 'index')
    ->create('users');

$record->setKey(array('name'), 'index', null, false)
    ->alter('users');

$record->truncate('users');

$record->drop('users');

try {
    $record->begin();
    $record->insert('users', array('name' => 'Roger Sherman'));
    $record->commit();
} catch (\PDOException $e) {
    $record->rollBack();
}