PHP SQL database layer
PHP
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/pjsql
test
.gitignore
README.md
composer.json

README.md

pajamaSQL

pajamaSQL is a PHP SQL database layer. It supports MySQL, PostgreSQL and SQLite.

Documentation

This README is currently the only documentation.

Requirements

PHP 5.3 or higher, Composer, and at least one of the follow PHP vendor specific database extensions: Mysqli, PostgreSQL, SQLite3.

Source Code

This project and its source code are available on GitHub.

Tests

All the tests are in the test directory. Each PHP script in the top level test directory is a separate test. You need to run composer install in the test directory before running any of the tests. You also need to setup database credentials and put them in a test before running any test.

Installation

Install using composer:

{
    "require": {
        "pajamasql/pajamasql": "0.5.0",
    }
}

Connect

Connect to MySQL:

$mysql = new pjsql\Mysql('host', 'username', 'password', 'db');

Connect to PostgreSQL:

$pgsql = new pjsql\Pgsql('dbname=mydb user=jon password=123456');

Connect to SQLite:

$sqlite = new pjsql\Sqlite('mydb.db');

exec()

Use exec() to execute a result-less SQL query:

$sqlite->exec('CREATE TABLE tword (word TEXT)');

query()

Use query() to get a 2d array of results:

var_dump($pgsql->query('SELECT * FROM tword'));

esc()

Use esc() to escape strings in SQL queries:

$mysql->exec(sprintf('INSERT INTO tword (word) VALUES("%s")',
    $mysql->esc('"hello"')));

conn() and error()

Use conn() to access the full vendor specific database extension:

if($status = $mysql->conn()->stat()) {
    echo $status;
}
else {
    //call error() when using conn()
    $mysql->error();
}

Models

Setup a model factory:

namespace myns;

class ModelFactory extends \pjsql\AdapterFactory {
    protected static function databaseHandle() {

        //all models share a single instance of this object
        return new \pjsql\Mysql('host', 'username', 'password', 'db');
    }
}

Create model(s):

namespace myns;

class WordModel extends \pjsql\DatabaseAdapter {
    public function install() {
        $this->exec('CREATE TABLE tword (
            word_id INT AUTO_INCREMENT PRIMARY KEY,
            word VARCHAR(32))');
    }

    public function create($word) {
        $this->exec(sprintf('INSERT INTO tword (word) VALUES("%s")',
            $this->esc($word)));
    }

    public function get() {
        return $this->query('SELECT * FROM tword');
    }
}

Get model(s) and call their methods:

//must pass full namespace to get()
$wordModel = myns\ModelFactory::get('myns\WordModel');

$wordModel->install();
$wordModel->create('pink');
$wordModel->create('bread');

var_dump($wordModel->get());

Errors

Handle SQL errors with set_exception_handler():

set_exception_handler(function($e) {
    if($e instanceof pjsql\DatabaseException) {
        die($e->getMessage());
    }
    else {
        throw $e;
    }
});

LICENSE

MIT http://ryf.mit-license.org/