You can try this...I made this a while back. It was made pretty quick and only to simplify matters for myself. I'm sure you could customize it to your needs.
<?php
/**
* Instead of using the constructor to set
* the db info. You can define them before hand.
**/
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_USER', 'user');
DEFINE('DB_PASS'
, 'pass');
DEFINE('DB_NAME', 'dbname');
class dbConn{
private $conn;
private $query;
public $rows = array();
public $entries;
public function __construct($hostname='',$username='',$password='',$db_name=''){
$hostname = (($hostname == '')?DB_HOST:$hostname);
$username = (($username == '')?DB_USER:$username);
$pwd = (($password == '')?DB_PASS:$password);
$db_name = (($db_name == '')?DB_NAME:$db_name);
if(!$this->conn = @mysql_connect($hostname,$username,$pwd,true)){
trigger_error('DB Connection Failed.', E_USER_NOTICE);
}
if(!mysql_select_db($db_name, $this->conn)){
trigger_error('Unable to select DB.', E_USER_NOTICE);
}
}
public function query($sql){
if(!$this->query = mysql_query($sql, $this->conn)){
trigger_error('Unable to execute query.<br />'.$sql.'<br />'.mysql_error(), E_USER_NOTICE);
return false;
}else{
return true;
}
}
public function last_id(){
return mysql_insert_id();
}
public function fetch(){
while($info = mysql_fetch_assoc($this->query)){
$this->rows[] = $info;
}
}
public function entry(){
$this->entries = mysql_num_rows($this->query);
}
public function close(){
$this->stop();
}
public function stop(){
mysql_close($this->conn);
}
}
?>
Usage...
/**
* Usage
**/
$db_conn = new dbConn;
$db_conn->query("SELECT id, name FROM table");
$db_conn->entry();
echo 'Total entries : ' . $db_conn->entries . "\n";
$db_conn->fetch();
foreach($db_conn->rows AS $row){
echo 'Id : ' . $row['id'] . "\n";
echo 'Name : ' . $row['name'] . "\n\n";
}
/**
* If insert is used in the query.
* Use $db_conn->last_id() to get
* the last auto_incremented ID.
**/
$db_conn->close(); //or $db_conn->stop();