Database Class - please comment
Posted: Tue Apr 12, 2005 9:29 pm
Here is a database class I am working on, I'd appreciate any feedback you can give me. Should I now create another class which provides interaction with the database? ie. update/remove functions etc, or should I place these functions in this existing class? thanks.
Code: Select all
<?php
/********************
* Copyright (c) 2005
* db.class.php
* Base database functions
********************/
// required files
require_once('config.php');
class db {
// class variables
var $results;
var $db;
var $host;
var $user;
var $pass;
/**
* @return void
* @desc db constructor
**/
function db() {
$this->db = DB_DB;
$this->user = DB_USER;
$this->host = DB_HOST;
$this->pass = DB_PASS;
// connect to database
$this->connect();
}
/**
* @return boolean
* @desc connects and selects database
**/
function connect() {
if (!@mysql_connect($this->host, $this->user, $this->pass)) {
// can't connect to database
error::log(ERR_MED, 'Unable to connect to '.$this->user.'@'.$this->host.':<br>'.mysql_error(), ERR_SHOW);
return false;
}
if (!@mysql_select_db(DB_DB)) {
// can't select database
error::log(ERR_MED, 'Unable to select database '.$this->db.':<br>'.mysql_error(), ERR_SHOW);
return false;
}
return true;
}
/**
* @return boolean
* @param string $query
* @desc performs selected query
**/
function query($query) {
if (!@mysql_query($query)) {
error::log(ERR_MED, 'Unable to execute query:<br>'.mysql_error(), ERR_SHOW);
return false;
}
return true;
}
/**
* @return array
* @param resource $result
* @param string $type
* @desc gets array of specified type
**/
function fetch_array($result, $type = 'MYSQL_ASSOC') {
// get results
$this->results = @mysql_fetch_array($result, $type);
if (!$this->results) {
// can't fetch array
error::log(ERR_MED, 'Unable to get result:<br>'.mysql_error(), ERR_SHOW);
}
return $this->results;
}
/**
* @return int
* @desc gives us number of rows
**/
function num_of_rows() {
$num = @mysql_num_rows($this->results);
if (!$num) {
return 0;
}
return $num;
}
}
?>