Login   Register  
PHP Classes
elePHPant
Icontem

File: class.db.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Kami Knx  >  True MySQL class  >  class.db.php  >  Download  
File: class.db.php
Role: Class source
Content type: text/plain
Description: True mySQL class
Class: True MySQL class
MySQL database access wrapper
Author: By
Last change: Some queries optimized.
Date: 6 years ago
Size: 6,937 bytes
 

Contents

Class file image Download
<?php
/**
 * mySQL class
 *
 * @package db
 * @subpackage mysql
 *
 */
    
class DB {
        private 
$db;
        private 
$host;
        private 
$user;
        private 
$pass;
        
        private 
$con;
        private 
$database;
        private 
$res;
        private 
$row;
        
/**
         * Number of current row
         *
         * @var int
         */
        
private $rowN = -1;

        
/**
         * Connect to database
         *
         * @param resource link[optional] con
         * @param string[optional] host
         * @param string[optional] db
         * @param string[optional] user
         * @param string[optional] pass
         * @return DB
         */
        
public function DB($con=NULL$host=DBHOST$db=DBDB$user=DBUSER$pass=DBPASS) {
            if (!
$con) {
                
$this->con mysql_connect($host$user$pass);
                
$this->database $this->selectDB($db);
                if (!
$this->database) {
                    
$this->__destruct();
                }
            }
            else {
                
$this->con $con;
            }
        }
        
        
/**
         * Select the database
         *
         * @param String database
         * @return boolean
         */
        
public function selectDB($database) {
            return 
mysql_selectdb($database$this->con);
        }
        
        
/**
         * Execute a query
         *
         * @param string sql
         * @param boolean[optional] insert
         * @return Resource/Last_id
         */
        
public function query($sql$insert=false) {
            
$this->rowN = -1;
            
$this->res mysql_query($sql$this->con);
            if (
$insert)
                return 
mysql_insert_id($this->con);
            return 
$this->res;
        }
        
        
/**
         * Get connection resource
         *
         * @return link resource
         */
        
public function getCon() {
            return 
$this->con;
        }
        
        
/**
         * Gets a row
         *
         * @return array
         */
        
public function getRow() {
            ++
$this->rowN;
            
$this->row mysql_fetch_array($this->res);
            return 
$this->row;
        }
        
        
/**
         * Gets a row in object format
         *
         * @param class class
         * @param array[optional] params
         * @return new object
         */
        
public function getRowAsObj($class$params=NULL) {
            ++
$this->rowN;
            
$this->row mysql_fetch_object($this->res$class$params);
            return 
$this->row;
        }
        
        
/**
         * Gets a row ASSOC
         *
         * @return array
         */
        
public function getRowAssoc() {
            ++
$this->rowN;
            
$this->row mysql_fetch_assoc($this->res);
            return 
$this->row;
        }
        
        
/**
         * Get data from field
         *
         * @param string field
         * @return string
         */
        
public function getField($field) {
            return 
$this->row[$field];
        }
        
        
/**
         * Gets a result from a field in row
         *
         * @param string[optional] field
         * @param int[optional] row
         * @return string
         */
        
public function getResult($field=""$row=0) {
            if (
$field != "")
                return 
mysql_result($this->res$row$field);
            else return 
mysql_result($this->res$row);
        }

        
/**
         * Return the name of idfield
         *
         * @return string
         */
        
public function getIdFieldName($table) {
            
$this->query("SHOW COLUMNS FROM $table");
            return 
mysql_fieldname($this->res0);
        }

        
/**
         * Return the name of a field
         *
         * @param string table
         * @param int fieldIndex
         * @return string
         */
        
public function getFieldName($table$fieldIndex) {
            
$this->query("SHOW COLUMNS FROM $table");
            return 
mysql_fieldname($this->res$fieldIndex);
        }
        
        
/**
         * Return the name of idfield
         *
         * @param string table
         * @return array
         */
        
public function getFieldNameList($table) {
            
$this->query("SHOW COLUMNS FROM $table");
            
$fields = array();
            
$i 0;
            while ((
$field $this->getRowAssoc())) {
                foreach (
$field as $type => $value) {
                    
$fields[$i][strtolower($type)] = $value;
                }
                ++
$i;
            }
            return 
$fields;
        }
        
        
/**
         * Get the number of fields of a table
         *
         * @param string table
         * @return int
         */
        
public function getNumFields($table) {
            
$this->query("SHOW COLUMNS FROM $table");
            
$i 0;
            while (
$this->getRowAssoc())
                ++
$i;
            return 
$i;
        }
        
        
/**
         * Get insert id
         *
         * @return mysql_insert_id
         */
        
public function getInsertId() {
            return 
mysql_insert_id($this->con);
        }
        
        
/**
         * Get max id
         *
         * @return last_id
         */
        
public function getMaxId($table) {
            
$idName $this->getIdFieldName($table);
            
$sql "SELECT MAX($idName) FROM $table";
            
$this->query($sql);
            return 
$this->getResult();
        }
        
        
/**
         * Jumps to the next row
         *
         */
        
public function seekForward() {
            if (
mysql_data_seek($this->res$this->rowN+1))
                ++
$this->rowN;
        }
        
        
/**
         * Jumps to the row
         *
         * @param int row
         */
        
public function seek($row) {
            if (
mysql_data_seek($this->res$row))
                
$this->rowN $row;
        }

        
/**
         * Jumps to the previous row
         *
         */
        
public function seekBackward() {
            if (
mysql_data_seek($this->res$this->rowN-1))
                --
$this->rowN;
        }

        
/**
         * Return n# of affected rows
         *
         * @return int
         */
        
public function getAffectedRows() {
            return 
mysql_affected_rows($this->con);
        }
            
        
/**
         * Close the connection
         *
         */
        
public function __destruct() {
            @
mysql_free_result($this->res);
            @
mysql_close($this->con);
        }
        
    }

?>