Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Database
- * Access databases using PDO and MySQL. Supports prepared statements.
- *
- * @package com.codeante.lib.base
- * @author GIDIX
- */
- class Database {
- private $db;
- public $lastQuery;
- /**
- * This variable determines whether {@see Database} should handle errors
- * or pass it on via an exception. If this is set to true, {@see Database} handles all errors.
- *
- * @var boolean
- */
- public $handleError = false;
- /**
- * @param string $host MySQL Host
- * @param string $username MySQL Username
- * @param string $password MySQL Password
- * @param string $database MySQL Database
- */
- public function __construct($host, $username, $password, $database) {
- $this->db = new PDO('mysql:host=' . $host . ';dbname=' . $database . ';charset=utf8', $username, $password);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- }
- public function query($sql, array $args = null) {
- try {
- if (!is_null($args)) {
- $this->lastQuery = $this->db->prepare($sql);
- $this->lastQuery->execute($args);
- } else {
- $this->lastQuery = $this->db->query($sql);
- }
- return $this->lastQuery;
- } catch (PDOException $e) {
- if ($this->handleError) {
- $this->error($e->getMessage(), $sql, $e->getCode());
- } else {
- throw $e;
- }
- }
- }
- /**
- * Prepare a statement and return it without executing.
- *
- * @param string $query Query to send.
- */
- public function prepare($sql) {
- try {
- return $this->db->prepare($sql);
- } catch (PDOException $e) {
- if ($this->handleError) {
- $this->error($e->getMessage(), $sql, $e->getCode());
- } else {
- throw $e;
- }
- }
- }
- /**
- * Fetch an object using its {@see PDOStatement} $stmt or the last query.
- *
- * @param PDOStatement $stmt (optional, null)
- *
- * @return stdClass
- */
- public function fetchObject(PDOStatement $stmt = null) {
- if (!is_null($stmt)) {
- return $stmt->fetch(PDO::FETCH_OBJ);
- } else {
- return $this->lastQuery->fetch(PDO::FETCH_OBJ);
- }
- }
- public function fetch(PDOStatement $stmt = null, $fetch_style = FETCH_ASSOC) {
- if (!is_null($stmt)) {
- return $stmt->fetch(PDO::$fetch_style);
- } else {
- return $this->lastQuery->fetch(PDO::$fetch_style);
- }
- }
- /**
- * Get the number of affected rows by $stmt or the last query.
- *
- * @param PDOStatement $stmt (optional, null)
- *
- * @return int
- */
- public function numRows(PDOStatement $stmt = null) {
- if (!is_null($stmt)) {
- return $stmt->rowCount();
- } else {
- return $this->lastQuery->rowCount();
- }
- }
- /**
- * Get the last inserted ID.
- *
- * @return int
- */
- public function insertID() {
- return $this->db->lastInsertId();
- }
- /**
- * Begin a transaction for committing and rollbacks.
- */
- public function beginTransaction() {
- return $this->db->beginTransaction();
- }
- /**
- * Commit a transaction.
- */
- public function commit() {
- return $this->db->commit();
- }
- /**
- * Rollback recent changes before they were commited.
- */
- public function rollback() {
- return $this->db->rollBack();
- }
- public function errorInfo() {
- return $this->db->errorinfo();
- }
- protected function error($msg, $sql, $code) {
- Helpers::log(Helpers::LOG_ERROR, $msg . '<br /><br /><code class="important">' . $sql . '</code>');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement