Created
October 6, 2017 21:26
-
-
Save MetaDone/2fcf6e85c221469e5f37819fb47d83ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$user = DbQueryExec::getInstance()->fetchQuery("select * from users where id=:id limit 1", ['id'=>666 ] ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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