<?php
class DbConnector {
/* DATABASE VARIABLES */
var $host;
var $user;
var $password;
var $dbname;
/* VARIABLES */
var $link = 0; // Connection ID to database
var $sql = ""; // The database query
var $result = 0; // The result of a query
var $result_number = 0; // Number of the results of a query
var $db_status = "disconnected"; // Status of the database connection
var $error = ""; // MySQL ERROR
/* RUN MAIN FUNKTIONS */
function DbConnector() {
$this->host = "host.net";
$this->user = "user";
$this->password = "pwd";
$this->dbname = "dbname";
$this->db_open();
}
/* CONNECT TO DATABASE */
function db_open() {
if($this->db_status != "connected") {
if(!mysql_connect($this->host, $this->user, $this->password)) {
$this->db_status = "disconnected";
$this->error .= " · db_open() - Connection to database failed.<br />";
return 0;
}
else {
$this->link = mysql_connect($this->host, $this->user, $this->password);
$this->db_status = "connected";
$this->db_select_db($this->dbname);
return 1;
}
}
else {
$this->error .= " · db_open() - DB is already used.<br />";
return 0;
}
}
/* PERSISTENT CONNECTION TO DATABASE */
function db_p_open() {
$this->close();
$this->free();
if(!($this->link = mysql_pconnect($this->host, $this->user, $this->password))) {
$this->db_status = "disconnected";
$this->error .= " · db_p_open() - Connection to database failed.<br />";
return 0;
}
else {
$this->db_status = "connected";
$this->db_select_db($this->dbname);
return 1;
}
}
/* SELCT DATABASE */
function db_select_db($dbname) {
if($this->db_status == "connected") {
if(!mysql_select_db($dbname)) {
$this->error .= " · db_select_db() - No database selected.<br />";
return 0;
}
else return 1;
}
else return 0;
}
/* CHECK CONNECTION */
function is_connected() {
return($this->db_status == "connected");
}
/* EXECUTE DATABASE QUERY */
function db_query($query) {
$this->sql=$query;
if($this->db_status == "connected") {
if($this->result = mysql_query($query)) {
$query = trim($query);
if(substr(strtoupper($query), 0, 4) == "SELE") $this->result_number = mysql_num_rows($this->result);
return 1;
}
else {
$this->error .= " · db_query() - Could not save query to class.<br />";
return 0;
}
}
else return 0;
}
/* RETURNS LATEST DATABASE QUERY */
function getQuery() {
if(empty($this->sql)) {
$this->error .= " · getQuery() - There is no query.<br />";
}
else {
return $this->sql;
}
}
/* RETURN ROW COUNT */
function getNumRows($result){
if($this->error()) {
$this->error .= " · getNumRows() - Please check your MySQL-query.<br /> ";
$return = null;
} else {
$return = mysql_num_rows($this->result);
}
return $return;
}
/* GET ARRAY OF QUERY RESULTS */
function db_save_results($typ = 0) {
if($this->error()) {
$this->error .= " · db_save_results() - Please check your MySQL-query.<br /> ";
$return = null;
}
else {
$array = array();
while ($result = $this->db_get_next_result($typ)) $array[] = $result;
return $array;
}
}
/* GET NEXT RESULT OF LAST QUERY */
function db_get_next_result($typ = 0) {
if($this->error()) {
$this->error .= " · db_get_next_result() - Please check your MySQL-query.<br /> ";
$return = null;
}
else {
if($typ == 0) return mysql_fetch_array($this->result);
if($typ == 1) return mysql_fetch_row($this->result);
if($typ == 2) return mysql_fetch_assoc($this->result);
}
}
/* TEST QUERY ID */
function error() {
$tmp = $this->result;
$tmp = (bool)$tmp;
$tmp = !$tmp;
return $tmp;
}
/* GET LAST ERRORS */
function db_get_last_error() {
if($this->error()) {
$str = "<br /><br /><hr /><b>ERROR:</b><br />".$this->error;
}
else {
$str = "";
}
return $str;
}
/* CLOSE CONNECTION */
function close() {
if($this->db_status == "connected") {
if(!mysql_close($this->link)) {
$this->error .= " · close() - Disconnection failed.<br />";
return 0;
}
else {
$this->db_status = "disconnected";
return 1;
}
}
else return 0;
}
/* FREE MEMORY */
function free() {
if($this->error()) {
$this->error .= " · free() - Please check your MySQL-query.<br />";
$return = null;
}
else {
mysql_free_result($this->result);
}
}
}
?>