Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Description of Database
- *
- * @author Ivaylo Ivanov
- */
- namespace Models;
- class Database {
- private $_pdo,
- $_query,
- $_error = false,
- $_result,
- $_count = 0;
- /**
- * Database params.
- *
- * @param type $host
- * @param type $dbname
- * @param type $username
- * @param type $password
- * @param type $charset
- */
- public function __construct($host, $dbname, $username, $password, $charset = 'utf8') {
- try {
- $this->_pdo = new \PDO("mysql:host=$host;dbname=$dbname;charset=$charset", $username, $password);
- } catch (\PDOException $e) {
- die($e->getMessage());
- }
- }
- /**
- * <code>
- * $params['sql'] = "SELECT * .....";
- * $params['bind'][':user_name']
- * $params['type']
- * </code>
- * @param type $sql
- * @param type $params
- * @param type $type
- * @return \Database
- */
- public function query($sql, $params = array(), $type = 5) {
- $this->_error = FALSE;
- if ($this->_query = $this->_pdo->prepare($sql)) {
- if (count($params)) {
- foreach ($params as $field => $values) {
- $this->_query->bindParam($field, $values['param'], $values['type']);
- }
- }
- if ($this->_query->execute()) {
- $this->_result = $this->_query->fetchAll($type);
- $this->_count = $this->_query->rowCount();
- } else {
- $this->_error = $this->_query->errorInfo();
- }
- }
- return $this;
- }
- /**
- *
- * @return type
- */
- public function getErrors() {
- return $this->_error;
- }
- /**
- *
- * @return type
- */
- public function getResult() {
- return $this->_result;
- }
- /**
- *
- * @return type
- */
- public function getCount() {
- return $this->_count;
- }
- /**
- *
- * @return type
- */
- public function lastID() {
- return $this->_pdo->lastInsertId();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment