leoshtika/database

A lightweight wrapper around PDO

v1.1.2 2016-02-27 15:43 UTC

README

License Packagist

Requirements

  • PHP 5.3 or higher

Installation with Composer

  • from the command line
composer require leoshtika/database
  • or updating your composer.json file
{
    "require": {
        "leoshtika/database": "~1.1"
    }
}

Usage

Connect to an SQLite database

<?php
require_once 'vendor/autoload.php';

use leoshtika\libs\Sqlite;
use leoshtika\libs\UserFaker;

$sqliteFile = 'demo.sqlite';

// Create the database if not exists
UserFaker::create($sqliteFile);

$dbh = Sqlite::connect($sqliteFile);

$sth = $dbh->prepare('SELECT * FROM user');
$sth->execute();
$users = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach ($users as $user) {
    echo $user['name'] . ' Email: ' . $user['email'];
    echo '<hr>';
}

Connect to a MySQL database

require_once 'vendor/autoload.php';

use leoshtika\libs\Mysql;

$config = array(
    'host' => 'localhost',
    'dbname' => 'myapp',
    'user' => 'root',
    'pass' => '',
);

$dbh = Mysql::connect($config);

$sth = $dbh->prepare('SELECT * FROM user');
$sth->execute();
$users = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach ($users as $user) {
    echo $user['name'] . ' Email: ' . $user['email'];
    echo '<hr>';
}

Enjoy!