You're more or less looking for a class.
Here's mine.
<?php
class dbConnection {
var $dbLoc;
var $dbUser;
var $dbPass;
var $dbName;
var $items = array();
var $result;
var $rows;
var $connection;
var $connect;
var $death;
var $sql;
function dbConnect(){
if (($this->connection = mysql_connect($this->dbLoc, $this->dbUser, $this->dbPass)) === false) {
echo mysql_error();
return false;
}
if (mysql_select_db($this->dbName, $this->connection) === false) {
echo mysql_error();
return false;
}
}
function dbQuery($sql){
if(get_magic_quotes_gpc()){
$this->sql = stripslashes(mysql_real_escape_string($sql));
} else {
$this->sql = mysql_real_escape_string($sql);
}
if(mysql_query($this->sql)){
return true;
} else {
return false;
}
}
function dbFetch($sql){
$this->items = array();
$this->result = mysql_query($sql, $this->connection);
$item = array();
while($item = mysql_fetch_array($this->result)){
$this->items[] = $item;
}
if(!mysql_error()){
return $this->items;
} else {
return mysql_error();
}
}
function dbRows($sql){
$this->result = mysql_query($sql, $this->connection);
$this->rows = mysql_num_rows($this->result);
if(!mysql_error()){
return $this->rows;
} else {
return mysql_error();
}
}
function dbKill(){
if(($this->death = mysql_close($this->connection)) === false){
return mysql_error();
}
}
}
?>