Skip to content

Instantly share code, notes, and snippets.

@MetaDone
Created October 6, 2017 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MetaDone/2fcf6e85c221469e5f37819fb47d83ce to your computer and use it in GitHub Desktop.
Save MetaDone/2fcf6e85c221469e5f37819fb47d83ce to your computer and use it in GitHub Desktop.
<?php
$user = DbQueryExec::getInstance()->fetchQuery("select * from users where id=:id limit 1", ['id'=>666 ] );
<?php
use PimpleSingleton\Container;
/**
* Собранный на коленке левой пяткой выполнятель запросов
*/
class DbQueryExec
{
private static $instance = null;
private $link;
/**
* @return DbConn
*/
public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __clone()
{
}
private function __construct()
{
/**
* Представим что мы используем https://github.com/pokmot/pimple-singleton
* и получаем коннект к базе через PDO как сервис
*/
$this->link = Container::getContainer()->get('db');
$this->link->exec("SET CHARACTER SET utf8");
}
public function fetchQuery($sql, array $params = [], int $fetchMode = PDO::FETCH_ASSOC)
{
$query = $this->execQuery($sql, $params);
$data = $query->fetchAll($fetchMode);
return $data;
}
/**
* Execute sql-statement
* @param type $sql
* @param type $params
* @return PDOStatement
*/
private function execQuery($sql, $params = [])
{
$query = $this->link->prepare($sql);
$query->execute($params);
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment