Giter Site home page Giter Site logo

easypdo's Introduction

EasyPDO

An easy to use PDO wrapper class.

1. Connection

    require_once("EasyPDO.php");
    $db = new EasyPDO('mysql:dbname=dummy;host=localhost;charset=UTF8', 'root', '');

2. CRUD methods

Basic functions for data manipulation.

select

    $data = $db->select('dummy', '*', 'id <= :max', array(':max'=>100), "id ASC", "0, 3");
    print_r($data);

insert

    $data = array(
        'inf1' => 'test2',
        'inf2' => 'insert'
    );
    $db->insert('dummy', $data);

update

    $data = array(
        'inf1' => 'test3',
        'inf2' => 'update'
    );
    $db->update('dummy', $data, 'id = :id', array(':id' => 3));

delete

    $db->delete('dummy', "id=:id" , array(':id' => 3));

truncate

    $db->truncate('dummy');

save

If exists then update, else insert.

    $data = array(
        'inf1' => 'test5',
        'inf2' => 'save'
    );
    $db->save('dummy', $data, "inf1=:inf1" , array(':inf1' => 'test5'));

bulkInsert

Insert multiple rows at a time.

    $columns = array('inf1', 'inf2');
    $data = array(
        array('value1', 'value2'),
        array('value3', 'value4'),
        array('value5', 'value6'),
    );
    $db->bulkInsert('dummy', $columns, $data);

3. Fetch methods

Just like fetch methods in Zend_Db.

fetchOne

    $sql = "SELECT inf1 FROM dummy WHERE id = :id";
    $bind = array(
        ':id' => 1
    );
    $data = $db->fetchOne($sql, $bind);

fetchRow

    $sql = "SELECT id, inf1, inf2 FROM dummy WHERE id <= :max ORDER BY id";
    $bind = array(
        ':max' => 100
    );
    $data = $db->fetchRow($sql, $bind);

fetchAll

    $sql = "SELECT id, inf1, inf2 FROM dummy WHERE id BETWEEN :start AND :end ORDER BY id";
    $bind = array(
        ':start' => 1,
        ':end' => 3
    );
    $data = $db->fetchAll($sql, $bind);

fetchAssoc

    $sql = "SELECT inf1, inf2 FROM dummy WHERE id BETWEEN :start AND :end ORDER BY id";
    $bind = array(
        ':start' => 1,
        ':end' => 3
    );
    $data = $db->fetchAssoc($sql, $bind);

fetchAssocArr

    $sql = "SELECT inf1, inf2 FROM dummy WHERE id BETWEEN :start AND :end ORDER BY id";
    $bind = array(
        ':start' => 1,
        ':end' => 3
    );
    $data = $db->fetchAssocArr($sql, $bind);

fetchPairs

    $sql = "SELECT inf1 AS name, inf2 AS value FROM dummy WHERE id BETWEEN :start AND :end ORDER BY id";
    $bind = array(
        ':start' => 1,
        ':end' => 3
    );
    $data = $db->fetchPairs($sql, $bind);

fetchCol

    $sql = "SELECT inf1 FROM dummy WHERE id BETWEEN :start AND :end ORDER BY id";
    $bind = array(
        ':start' => 1,
        ':end' => 3
    );
    $data = $db->fetchCol($sql, $bind);

4. Transaction methods

Same as original PDO class, but can be nested.

Example

    try {
        $db->beginTransaction();
        for($i=0; $i < 5; $i++) {
            $db->insert(
                'dummy',
                array(
                    'id'=> rand(1, 10000), 
                    'inf1'=> 'random id', 
                    'inf2'=> 'transaction test'
                )
            );
        }
        $db->commit();
    } catch(PDOException $e) {
        $db->rollback();
    }

easypdo's People

Contributors

jlake avatar

Watchers

James Cloos avatar Michael Seeger avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.