Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- var $connected = 0;
- var $db_type = DATABASE_TYPE;
- var $db_hostname = DATABASE_HOST;
- var $db_username = DATABASE_USER;
- var $db_password = DATABASE_PASS;
- var $db_database = DATABASE_DATA;
- function __construct($type = null, $hostname = null, $username = null, $password = null) {
- if (($type != null) && ($hostname != null) && ($username != null) && ($password != null)) {
- $this->db_type = $type; // Add initial support for diff kinds of db types
- $this->db_hostname = $hostname;
- $this->db_username = $username;
- $this->db_password = $password;
- }
- }
- public function Test() {
- if ($this->Connect () == true) {
- unset ( $this->pdo );
- return true;
- }
- }
- public function Select_DB($db) {
- $this->db_database = $db;
- return $this->db_database;
- }
- public function Connect() {
- // Check for database type, for now we start with mysql support only, this can be extended
- if ($this->db_type == strtolower ( "mysql" )) {
- try {
- $this->pdo = new PDO ( "mysql:host=$this->db_hostname;dbname=$this->db_database", $this->db_username, $this->db_password );
- } catch ( PDOException $Exception ) {
- $this->connected = 0;
- throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
- }
- $this->connected = 1;
- return true; // We are connected and database is selected, return true
- }
- }
- public function Bind() {
- $this->bind = func_get_args ();
- }
- public function Fetch($what, $from, $where = null, $i = 1) {
- if ($where == null)
- $query = 'SELECT ' . $what . ' FROM ' . $from . '';
- else
- $query = 'SELECT ' . $what . ' FROM ' . $from . ' ' . $where . '';
- try {
- if ($this->connected == 0)
- $this->Connect ();
- $this->stmt = $this->pdo->prepare ( $query );
- if (isset ( $this->bind ))
- foreach ( $this->bind as $key => $arg )
- $this->stmt->bindValue ( $i ++, $arg );
- $this->stmt->execute ();
- } catch ( PDOException $Exception ) {
- throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
- }
- return $this->stmt->fetchALL ( PDO::FETCH_OBJ );
- }
- public function Update($table, $set, $where = null, $i = 1) {
- if ($where == null)
- $query = "UPDATE $table SET $set";
- else
- $query = "UPDATE $table SET $set $where";
- try {
- if ($this->connected == 0)
- $this->Connect ();
- $this->stmt = $this->pdo->prepare ( $query );
- if (isset ( $this->bind ))
- foreach ( $this->bind as $key => $arg )
- $this->stmt->bindValue ( $i ++, $arg );
- $this->stmt->execute ();
- } catch ( PDOException $Exception ) {
- throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
- }
- return $this->stmt->rowCount ();
- }
- public function Insert($table, $name, $data, $i = 1) {
- $query = "INSERT INTO $table ( $name ) VALUES ( $data )";
- try {
- if ($this->connected == 0)
- $this->Connect ();
- $this->stmt = $this->pdo->prepare ( $query );
- if (isset ( $this->bind ))
- foreach ( $this->bind as $key => $arg ) {
- $this->stmt->bindValue ( $i ++, $arg );
- }
- $this->stmt->execute ();
- } catch ( PDOException $Exception ) {
- throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
- }
- return $this->stmt->rowCount ();
- }
- }
- ?>
Add Comment
Please, Sign In to add comment