MeekroDB Quick Start Docs ORM Docs FAQ
Download GitHub

MeekroDB

The Simple PHP MySQL Library

What is MeekroDB?

MeekroDB is a PHP MySQL library that will let you get more done with fewer lines of code while making SQL injection 100% impossible.

MeekroDB has been a top search result for "php mysql library" since 2013, and has thousands of deployments worldwide.

MeekroDB has a perfect security track record. No bugs relating to security or SQL injection have ever been discovered.

MeekroDB also works with SQLite and Postgres.

What is MeekroORM?

MeekroORM is an Object-Relational Mapper ("ORM") that comes with MeekroDB. It lets you access a database through objects rather than traditional queries.

MeekroORM is designed to be zero-configuration and dead-simple to use.

MeekroORM rejects complicated setup. If you can write this one line, you can use a powerful ORM: class Account extends MeekroORM {}

Learn More About MeekroORM

Examples (with MeekroDB)

Select Multiple Rows

Run a query and get back multiple rows with one line of code. The inputs are properly escaped, and you get back an array of assoc arrays that's easy to work with!

$results = DB::query("SELECT * FROM accounts WHERE username=%s AND password=%s", $username, $password);
foreach ($results as $result) {
  echo $result['username'] . '-' . $result['password'] . "\n";
}

Select Single Row

Sometimes you just need one row. This returns an assoc array.

$user = DB::queryFirstRow("SELECT * FROM accounts WHERE username=%s", $username);

Select Single Field

Need just one field? Don't waste time with the rest!

$count = DB::queryFirstField("SELECT COUNT(*) FROM accounts");

Insert an Assoc Array

Don't waste time trying to format an INSERT property, just pass an assoc array. You can also do INSERT IGNORE, UPDATE, and even INSERT .. ON DUPLICATE KEY UPDATE with a similar syntax!

DB::insert('accounts', [
  'username' => $username,
  'password' => $password,
]);

Nested Transactions

Get nested transactions with very simple syntax! MeekroDB will transparently convert inner transactions to SAVEPOINTS!

DB::$nested_transactions = true;
DB::startTransaction(); // START TRANSACTION
// .. some queries..
$depth = DB::startTransaction(); // SAVEPOINT PT1
echo $depth . 'transactions are currently active'; // 2

// .. some queries..
DB::commit(); // RELEASE SAVEPOINT PT1
// .. some queries..
DB::commit(); // commit outer transaction

Prefer Object-Oriented?

Most projects only use one database and don't need multiple database objects. If you do, you can create them like this.

$mdb = new MeekroDB('localhost', 'username', 'password');
$result = $mdb->query("SELECT * FROM login WHERE username=%s AND password=%s", 
  $username, $password);

Examples (with MeekroORM)

With an ORM (object-relational mapper) you deal with objects instead of database rows. For many kinds of applications, this results in simpler and more readable code. For the following examples, let's assume we have an accounts class with fields username, password, is_disabled, and unpaid_balance.

Set up your class

Just create an empty class, and MeekroORM will automatically discover your table structure. It also assumes that the table name is just your class name with an "s" at the end.

class Account extends MeekroORM {}

Save an object (insert a row)

Just set your fields and save it. It'll be inserted.

$Account = new Account();
$Account->username = 'Joe';
$Account->password = 'mypass';
$Account->Save();

Load an object by primary key

Let's load an object by primary key, update its password, and save it.

$Account = Account::Load(1);
$Account->password = $newpass;
$Account->Save();

Search for an object

$Account = Account::Search(['username' => 'Joe']);
$Account->password = $newpass;
$Account->Save();

Search for many objects

Let's extend the Account class a bit:

class Account extends MeekroORM {
    function disable() {
        $this->is_disabled = 1;
        $this->Save();
    }
}

And now let's use the new method we added to disable any accounts that have a large unpaid balance. You can actually search for any SQL query, as shown here.

$Accounts = Account::SearchMany('SELECT * FROM accounts WHERE unpaid_balance>%i', 100);
foreach ($Accounts as $Account) {
    $Account->disable();
}

Ready to give it a try?

Check out our Quick Start Guide!