Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class database extends PDO {
- private $host=db_host;
- private $dbname=db_name;
- private $user=db_user;
- private $pass=db_pass;
- private $dbh; //database handler
- private $error;
- private $stmt; // statement
- public function __construct()
- {
- //Устанавливаем dsn
- $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
- //Определяем параметры
- $options = array(
- PDO::ATTR_PERSISTENT => true, // постоянное подключение
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
- );
- //Создаем instance
- try {
- $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
- }
- // Обрабатываем ошибки
- catch (PDOException $e) {
- $this->error = $e->getMessage();
- }
- }
- public function __destruct()
- {
- //Закрываем соединение с базой
- $dbh=null;
- }
- public function query($query){
- $this->stmt = $this->dbh->prepare($query);
- }
- public function bind($param, $value, $type = null){
- //Определяем тип параметров
- if (is_null($type)) {
- switch (true) {
- case is_int($value):
- $type = PDO::PARAM_INT;
- break;
- case is_bool($value):
- $type = PDO::PARAM_BOOL;
- break;
- case is_null($value):
- $type = PDO::PARAM_NULL;
- break;
- default:
- $type = PDO::PARAM_STR;
- }
- $this->stmt->bindValue($param, $value, $type);
- }
- }
- public function execute(){
- return $this->stmt->execute();
- }
- //Возвращает массив с результатами
- public function resultset(){
- $this->execute();
- return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- //Возвращает единичную строку с результатом
- public function single(){
- $this->execute();
- return $this->stmt->fetch(PDO::FETCH_ASSOC);
- }
- //Возвращает количество найденных рядов
- public function rowCount(){
- return $this->stmt->rowCount();
- }
- //Возвращает id последнего insert
- public function lastInsertId(){
- return $this->dbh->lastInsertId();
- }
- public function debugDumpParams(){
- return $this->stmt->debugDumpParams();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement