Introducing Phrap

August 28, 2011  |  Posted in: Programming  |  0 comments

Tags: / /

Phrap - a super-light PHP database wrapper using PDO for basic CRUD

If that doesn’t sound sexy, well, that’s because it isn’t

I often write light web-service type apps that require simple DB access, but are far to simple to spin up a big ORM like Doctrine. Phrap is a tiny library (script) that provides simple read/write/delete to the DB using PDO to handle escaping and such.

It reads, it writes, it deletes. It doesn’t do associations, joins or validations, unless you feel like forking and adding. It keeps you from having to write a lot of SELECT * FROM blah blah blah and $stmt->prepare() every time you need something from your database, yet its stupid small so it doesn’t drag on your resources (much).

This was something I needed for myself but I also used this little project to teach myself PHPUnit, and yes, I now see some of the benefits of testing your software.

Speaking of which, read the tests to get a better idea how it works.

Get it Fresh on the GitHubs

A little Phrap:

class FileUpload extends Model
{
    public $table = "files";
    public $id;
    public $filename;
    public $userid;
    public $email;
 }

// Create
$dbh = new DB($database);
$file = new FileUpload($dbh);
$file->create();
$file->set(array('filename' => 'heffalump.txt', 'codswoddle' => 'phiminster'));
$file->save();

// Query
$result = $file->find('first');
$result = $file->find('all');
$result = $file->find('first', array('email' => 'dazza@email.com'));
$result = $file->query(
     'SELECT * FROM files WHERE email = :email ORDER BY userid LIMIT 1',
     array(':email' => 'dazza@email.com')
 );

// Delete manually by id
$file->delete(1);

// Set id in object then delete it
$file->find('first');
$file->delete();

Make a comment

Privacy: I will never give/sell/share your email address with anyone, ever. I need it to help crack down on spam, and contact you if I have a question about your comment that would be best handled in private.

A red label and/or an asterisk (*) indicates a required field

this will never be made public
(optional)

Preview:

Recent posts

Introducing Phrap

0 Comments :: August 28, 2011

Compile Ruby 1.9.2 with RVM using Homebrew installed Iconv

1 Comments :: January 30, 2011

Bending CSVs to your will with Ruby

0 Comments :: September 05, 2010

Git Down

0 Comments :: August 21, 2010

It's been a long time coming.

0 Comments :: February 01, 2010

Search the posts

Categories

Browse around

© 2012 Darren Newton, all rights reserved - Revision 334