Skip to content

Instantly share code, notes, and snippets.

@pomle
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save pomle/ba45036f625567b9c940 to your computer and use it in GitHub Desktop.

Select an option

Save pomle/ba45036f625567b9c940 to your computer and use it in GitHub Desktop.
<?php
// Init
$PDO = new \PDO($dsn, $user, $pass);
$DB = new \Asenine\Database\Connection($PDO);
try {
$DB->begin();
// Simple execution
$DB->execute("UPDATE users SET
boolean = %b,
string = %s,
signed = %d,
datetime = %t,
decimal = %f
WHERE
id IN %a",
true,
"this will be properly escaped using 'PDO::quote()'",
-1234,
2013.1151,
array(1,5,124,1)));
// Fetch PDOResult
$Result = $DB->execute("SELECT * FROM table");
// Fetch one row
$row = $DB->execute("SELECT * FROM table LIMIT 1")->fetch();
// Fetch one value
$value = $DB->execute("SELECT value FROM table LIMIT 1")->fetchObject()->value;
// Structured query (DB::select() returns Database\Query\Select)
$Query = $DB->select("
u.id AS user_id,
u.is_enabled,
u.is_administrator,
u.username,
u.fullname,
u.time_last_login,
u.time_modified,
u.count_logins_successful,
u.count_logins_failed,
sc.name AS company,
su.market_code")
->from("asenine_users u")
->join("scatman_users su ON su.user_id = u.id", 'LEFT')
->join("scatman_companies sc ON sc.id = su.company_id", 'LEFT');
if ($filter) {
$Query->where('su.company_id = %d', $filter);
}
$DB->commit();
} catch {
$DB->rollback();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment