Skip to content

Instantly share code, notes, and snippets.

@noodlehaus
Last active August 29, 2015 14:06
Show Gist options
  • Save noodlehaus/d8868d1713389f6dc96b to your computer and use it in GitHub Desktop.
Save noodlehaus/d8868d1713389f6dc96b to your computer and use it in GitHub Desktop.
minsql concept
<?php
namespace noodlehaus\minsql;
# create pdo conn, or get last used, or null
function connect(...$args) {
static $pdo = null;
if (!count($args))
return $pdo;
$pdo = new PDO(...$args);
if (PDO::ATTR_DRIVER_NAME !== 'mysql') {
return trigger_error(
"noodlehaus\minsql only works with MySQL.",
E_USER_ERROR
);
}
return $pdo;
}
# executes a query, and returns the statement
function query($sql_or_pdo, ...$args) {
$sql = null;
$pdo = null;
if ($sql_or_pdo instanceof PDO) {
$pdo = $sql_or_pdo;
$sql = array_shift($args);
} else {
$pdo = connect();
$sql = $sql_or_pdo;
}
$sql = $pdo->prepare($sql);
$sql->execute($args);
return $sql;
}
# returns all rows from the statement
function all(PDOStatement $sql) {
$all = $sql->fetchAll(PDO::FETCH_ASSOC);
$sql->closeCursor();
return $all;
}
# returns next row from the statement
function row(PDOStatement $sql) {
$row = $sql->fetch(PDO::FETCH_ASSOC);
($row === false) && $sql->closeCursor();
return $row;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment