<?php
// +------------------------------------------------------------------+
// | evo - Mysql DB Class
// | Copyright (c) 2005-2006 Evo-Dev
// +------------------------------------------------------------------+
class sqldb
{
var $scriptname = "";
/* login */
var $user = "";
var $pass = "";
var $server = "";
var $database = "";
/* others */
var $query_id = 0; // query id
var $query_counter = 0;
var $link_id = 0;
var $use_persistent = 0;
var $insert_into = "";
var $insert_data = array();
var $log_file = "";
var $use_log = "";
var $maxsize = "";
function sqldb()
{
$this->log_file = 'out/sqllog.txt';
$this->use_log = 0; // set 0 to disable
$this->maxsize = 0.1; //in mb, set 0 to disable (WARNING, it can get VERY LARGE)'
}
/*
START CONNECT
*/
function start($server,$user,$password,$database)
{
$this->server = $server;
$this->user = $user;
$this->pass = $password;
$this->database = $database;
$this->perform_connect();
}
/*
PERFORM CONNECT
*/
function perform_connect()
{
if ($this->use_persistent == 0)
{
$this->link_id = $this->connect($this->server,$this->user,$this->pass);
}
else
{
$this->link_id = $this->pconnect($this->server,$this->user,$this->pass);
}
if (!$this->link_id)
{
$this->error("Connection to Database ".$this->database." Failed");
}
if( !@$this->select_db($this->database, $this->link_id) )
{
$this->error("mySQL database (".$this->database.")cannot be used");
}
unset ($this->password);
}
/*
CONNECT
*/
function connect( $server, $user, $pass )
{
return mysql_connect( $server, $user, $pass );
}
/*
PCONNECT
*/
function pconnect( $server, $user, $pass )
{
return mysql_pconnect( $server, $user, $pass );
}
/*
SELECT DB
*/
function select_db($database,$link_id)
{
$res = mysql_select_db($database,$link_id);
mysql_query("SET NAMES utf8",$link_id);
return $res;
}
/*
SHOW ALL QUERIES
- for debug purposes
*/
function show_all()
{
return "<br /><br /><b> Debug Mode - All Queries :</b><hr size='1'> ".$this->query_show."<br />";
}
/*
QUERY
*/
function query($string)
{
if (trim($string != ""))
{
$this->query_counter++;
$this->query_show .= stripslashes($string)."<hr size='1'>";
$this->query_id = mysql_query($string,$this->link_id);
}
if (!$this->query_id) {
$this->error("mySQL Error on Query : ".$string);
}
if ( $this->use_log == 1 && $this->dontlog != 1 )
{
$this->addlog( $string, $this->note );
$this->note = '';
}
return $this->query_id;
}
/*
ADD INTO QUERY LOG
*/
function addlog ( $query, $note='' )
{
/*
var $log_file = 'out/sqllog.txt';
var $use_log = 1; // set 0 to disable
var $maxsize = 2;
*/
if ( $this->use_log == 1 )
{
$data = '------------------------------------------'."\n".date('Ymd G:i')."\n".$query."\n".($note != '' ? "Note: ".$note:'')."\n";
if ( !file_exists( $this->log_file) )
{
fputs(fopen ( $this->log_file, 'w'), $data);
}
else
{
if ( filesize( $this->log_file) > (($this->maxsize*1024)*1024) && $this->maxsize > 0)
{
@unlink( $this->log_file );
}
//$olddata = implode('',file($this->log_file));
fputs( fopen( $this->log_file, 'a'), $data);
}
}
}
/*
QUERY - INSERT
*/
function query_insert()
{
if ( $this->insert_into && (is_array($this->insert_data)) )
{
$this->query_counter++;
$this->query_show .= stripslashes($string)."<hr size='1'>";
foreach ($this->insert_data as $key => $val)
{
$value .= $key."='".$val."',";
}
if ( (substr($value,-1)) == "," )
{
$value = substr($value,0,-1);
}
$this->query_id = mysql_query("INSERT INTO ".$this->insert_into." SET $value");
}
if (!$this->query_id) {
$this->error("mySQL Error on Query : ".$string);
}
return $this->query_id;
}
/*
ESCAPE STRING
*/
function escape_string($value)
{
// values should be stripped if stupid magic quotes are enabled
if ( get_magic_quotes_gpc() )
{
$value = stripslashes($value);
}
$value = mysql_real_escape_string($value);
return $value;
}
function escape_string_array(&$array)
{
if( is_array($array) )
{
reset($array);
while( list($key,$val) = each($array) )
{
if ( is_string($val) )
{
$array[$key] = $this->escape_string($val);
}
elseif( is_array($val) )
{
$array[$key] = $this->escape_string_array($val);
}
}
}
return $array;
}
/*
FETCH ARRAY
*/
function fetch_array($query_id=-1)
{
if ($query_id != -1)
{
$this->query_id = $query_id;
}
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
/*
FETCH ASSOC
*/
function fetch_assoc($query_id=-1)
{
if ( $query_id != -1)
{
$this->query_id = $query_id;
}
$this->result = mysql_fetch_assoc( $this->query_id );
return $this->result;
}
/*
FETCH ROW
*/
function fetch_row($query_id=-1)
{
if ($query_id != -1)
{
$this->query_id = $query_id;
}
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
/*
FREE RESULT
*/
function free_result($query_id=-1)
{
if ($query_id != -1)
{
$this->query_id = $query_id;
}
return @mysql_free_result($this->query_id);
}
/*
QUERY AND RETURN ARRAY OF FIRST ROW
*/
function query_first( $query_string )
{
$this->query($query_string);
$get = $this->fetch_array($this->query_id);
$this->free_result($this->query_id);
return $get;
}
// FOR OLD SCRIPTS
function query_once( $query )
{
return $this->query_first( $query );
}
/*
NUM_ROWS
*/
function num_rows($query_id=-1)
{
if ($quert_id != -1)
{
$this->query_id=$query_id;
}
return mysql_num_rows($this->query_id);
}
/*
INSERT ID
*/
function insert_id()
{
return mysql_insert_id($this->link_id);
}
/*
AFFECTED ROWS
*/
function affected_rows()
{
return mysql_affected_rows($this->link_id);
}
/*
NUM_FIELDS
*/
function num_fields($query_id=-1)
{
if ($quert_id != -1)
{
$this->query_id=$query_id;
}
return mysql_num_fields($this->query_id );
}
/*
ERROR DESCRIPTION
*/
function get_errordesc()
{
$this->error=mysql_error();
return $this->error;
}
/*
ERROR NO
*/
function get_error_no()
{
$this->errorno=mysql_errno();
return $this->errorno;
}
/*
CLOSE CONNECTION
*/
function close()
{
mysql_close($this->link_id);
}
/*
ERROR
- output error message
*/
function error($msg)
{
global $_SERVER,$root,$settings;
$this->error_desc = mysql_error();
$this->error_no = mysql_errno();
if ($this->report != 1)
{
$the_error = "<b>mySQL WARNING!</b><br />";
$the_error .= "DB Error $this->scriptname: $msg <br /> More Information: <br /><ul>";
$the_error .= "<li> Mysql Error : ".$this->error_desc."</li>";
$the_error .= "<li> Mysql Error no # : ".$this->error_no."</li>";
$the_error .= "<li> Date : ".date("F j, Y, g:i a")."</li>";
$the_error .= "<li> Referer: ".$_SERVER['HTTP_REFERER']."</li>";
$the_error .= "<li> Script: ".$_SERVER['REQUEST_URI']."</li>";
$the_error .= '</ul>';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
if ($settings['sqlerror_email'] == 1)
{
@mail($settings['email'],$settings['sitename'].": Database Error", $the_error, $headers );
}
echo $the_error;
die();
}
else
{
$this->report_error = $msg;
}
}
}