Login   Register  
PHP Classes
elePHPant
Icontem

File: database.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of kancha  >  database  >  database.php  >  Download  
File: database.php
Role: ???
Content type: text/plain
Description: MySQL database class
Class: database
An abstraction layer to the MySQL database
Author: By
Last change:
Date: 14 years ago
Size: 1,883 bytes
 

Contents

Class file image Download
<?php
  /* 
      Class Database
      (c) Kancha
      kancha@flashmail.com
      
      Oct. 13, 2000
      
  */
    
class Database{
  
  // private instant variables
  var $dbConnectionID;
  var $queryID;
  var $record; 
  var $host;
  var $database;
  var $user;
  var $password;
  
  /* 
    constructor
    connect to datbase server and select specified database
  */
  function Database($host="localhost", $db="mydb", $user="kancha", $pwd="mypwd"){
    $this->host = $host;
    $this->database = $db;
    $this->user = $user;
    $this->password = $pwd;
    $this->connect();
  }
  
  
  /*
    private method
    used internally to generate dbConnectionID
  */
  function connect(){
    $this->dbConnectionID = @mysql_pconnect($this->host, $this->user, $this->password);
    if(!$this->dbConnectionID){
      echo(mysql_errno().":".mysql_error());
      exit;
    }
    else{
      $status = @mysql_select_db($this->database, $this->dbConnectionID);
      if(!$status){
        echo(mysql_errno().":".mysql_error());
        exit;
      }
    } 
  }
  
  
  //  public methods
  
  function query($sql){
    // connect to db incase connection id is not set
    if(empty($this->dbConnectionID))
      $this->connect();
    $this->queryID = @mysql_query($sql, $this->dbConnectionID);
    
    // handle error
    if(!$this->queryID){
      echo(mysql_errno().":".mysql_error());
      exit;
    }
  }  
  
  function nextRecord(){
    $this->record = @mysql_fetch_array($this->queryID);
    $status = is_array($this->record);
    return($status);
  }
  
  function numRows(){
    $rows = @mysql_num_rows($this->queryID);
    return($rows);
  }
  
  // get record field value from the current record pointed by $record
  function getField($field){
    return($this->record[$field]);
  }
}
?>