PHP code example of erykai / database

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

    

erykai / database example snippets


namespace Erykai\Database;

class Users extends Database
{
    public function __construct()
    {
        parent::__construct(
            'users',
            ['name', 'email', 'age'],
            'id'
        );
    }
}

const CONN_USER = 'root';
const CONN_PASS = 'root';
const CONN_BASE = 'erykai';
const CONN_HOST = 'mysql';
const CONN_DSN = 'mysql';

use Erykai\Database\Users;
hp";

$user = new Users();
$user->name = "Alex de Oliveira Vidal";
$user->email = "[email protected]";
$user->password = "102asda030";
$user->age = 10;
$user->save();
var_dump($user->response(), $user->data());

use Erykai\Database\Users;

ETURN ALL
$users = $Users->find()->fetch(true);
foreach ($users as $user) {
    echo "O $user->name existe!</br>";
}

//RETURNS THE FIRST RESULT FOUND
$user = $Users->find()->fetch();
echo "O $user->name existe!</br>";

//RETURNS THE FIRST RESULT OF THE QUERY
$user = $Users->find('name, email', 'name=:name', ['name'=>'Leonardo'])->fetch();
echo "O $user->name existe!</br>";

//RETURNS ALL QUERY RESULTS
$users = $Users->find('name, email', 'name=:name', ['name'=>'Leonardo'])->fetch(true);
foreach ($users as $user) {
    echo "O $user->name existe!</br>";
}

//RETURNS ALL QUERY RESULTS IN ORDER
$users = $Users
    ->find('name, email', 'name=:name', ['name'=>'Leonardo'])
    ->order("email", "DESC")
    ->fetch(true);
foreach ($users as $user) {
    echo "O $user->name existe!</br>";
}

//RETURNS QUERY RESULTS LIMIT
$users = $Users
    ->find('name, email', 'name=:name', ['name'=>'Leonardo'])
    ->limit(2)
    ->fetch(true);
foreach ($users as $user) {
    echo "O $user->name existe!</br>";
}

//RETURNS QUERY RESULTS LIMIT USING OFFSET
$users = $Users
    ->find('name, email', 'name=:name', ['name'=>'Leonardo'])
    ->limit(2)
    ->offset(2)
    ->fetch(true);
foreach ($users as $user) {
    echo "O $user->name existe!</br>";
}

//RETURNS GROUPED QUERY RESULTS
$users = $Users
    ->find('name, email', 'name=:name', ['name'=>'Leonardo'])
    ->group('name')
    ->fetch(true);
foreach ($users as $user) {
    echo "O $user->name existe!</br>";
}

//RETURNS RESULTS FROM MORE THAN ONE TABLE
$users = $Users
    ->find('name, email, address', 'name=:name', ['name'=>'Leonardo'])
    ->inner('INNER JOIN address ON id_user = users.id')
->fetch(true);
foreach ($users as $user) {
    echo "O $user->name existe e mora reside no endereço: $user->address!</br>";
}

//response
var_dump($Users->response());


use Erykai\Database\Users;
hp";

$user = new Users();
$email = "[email protected]";
$user->find('*', 'email=:email',['email'=>$email])->fetch();
$users = $user->data();
$users->email = "[email protected]";
$user->save();
var_dump($user->response());

use Erykai\Database\Users;
hp";

$updateAll = new Users();
$updateAlls = $updateAll->find('email', 'email=:email', ['email'=>'[email protected]'])->fetch(true);

foreach ($updateAll->data() as $userUpdate) {
    $user = new Users();
    $email = $userUpdate->email;
    $user->find('*', 'email=:email',['email'=>$email])->fetch();
    $users = $user->data();
    $users->email = "[email protected]";
    $user->save();
    var_dump($user->response());
}

use Erykai\Database\Users;
hp";

$user = new Users();
$user->find('id, name', 'id=:id', ['id'=>6])->fetch();
$data = $user->data();
$user->delete($data->id);
var_dump($user->response());

use Erykai\Database\Users;
hp";

$removeAll = new Users();
$removeAlls = $removeAll->find('id', 'email=:email',['email'=> '[email protected]'])->fetch(true);
foreach ($removeAll->data() as $userDel) {
    $user = new Users();
    $user->find('id, name', 'id=:id', ['id'=>$userDel->id])->fetch();
    $data = $user->data();
    $user->delete($data->id);
    var_dump($user->response());
}