00001 <?php if(!function_exists('startedIndexPhp')) { header("location:../index.php"); exit();}
00002
00003
00013 define('DEBUG_SQL_DISPLAY', 0);
00014
00015 interface sql_interface{
00016
00017 function __construct($tmpserver='', $tmpuser='', $tmppwd='', $tmpdb='');
00018
00019 # function error($msg=false);
00020
00021 function connect();
00022
00023 function selectdb();
00024
00025 function execute($tmp_query);
00026
00027 function fetchArray($type = "MYSQL_ASSOC");
00028
00029 function fetchRow();
00030
00031 function secure($var);
00032
00033 function lastId();
00034 }
00035
00036 # Class to connect the database
00037 class sql_class implements sql_interface{
00038 # Attributes to connect DB
00039 private $server; # string: DB Server
00040 private $user; # string: DB User
00041 private $pwd; # string: DB Password
00042 private $database; # string: DB database
00043 private $connect=false; # resource: DB connection
00044 private $result; # resource: result SQL Query
00045 private $lastId; # integer/string: last Id
00046
00047 # Attributes to execute queries
00048 public $error; # string: Error message
00049 public $errno; # integer: error no
00050
00051 ## Following attributes probably not used by Streber... ##
00052 public $tables; # array: Table(s) (for queries)
00053 public $fields; # array: Field(s) (for queries)
00054 public $where; # string: Where statement (for queries)
00055 public $order; # string: Order by (for queries)
00056 public $asc; # string: Order direction (for queries)
00057 public $from; # integer: First result (for queries)
00058 public $nbr; # integer: Nbr results (for queries)
00059 public $query; # string: Final query
00060
00064 function __construct($tmpserver='', $tmpuser='', $tmppwd='', $tmpdb=''){
00065
00066 $this->lastId = 0;
00067 $this->error = false;
00068 $this->errno = false;
00069 $this->server = $tmpserver;
00070 $this->user = $tmpuser;
00071 $this->pwd = $tmppwd;
00072 $this->database = $tmpdb;
00073 if(!function_exists('mysql_connect')){
00074
00075 if(function_exists('mysql_error')) {
00076 $this->error = mysql_error();
00077 }
00078 if(function_exists('mysql_errno')) {
00079 $this->errorno = mysql_errno();
00080 }
00081 trigger_error('Function mysql_connect() does not exists. mysql extension is not enabled?', E_USER_ERROR);
00082 return false;
00083 }
00084 return true;
00085 }
00086
00090 private function error($msg=false){
00091 $this->error = $msg;
00092 }
00093
00097 public function connect(){
00098 if($this->connect = @mysql_connect(
00099 $this->server,
00100 $this->user,
00101 $this->pwd
00102 )){
00103 $this->error('Connection using mysql_connect SUCCESSFUL');
00104 return true;
00105 }
00106 $this->error('Connection using mysql_connect FAILED');
00107 return false;
00108 }
00109
00110 public function selectdb(){
00111 if(@mysql_select_db($this->database)){
00112 $this->error('Base '.$this->database.' exists');
00113 return true;
00114 }
00115 $this->error('Base '.$this->database.' does NOT exist');
00116 return false;
00117 }
00118
00122 public function execute($tmp_query){
00123 global $g_count_db_statements;
00124 $g_count_db_statements++;
00125
00126 if($this->result = @mysql_query($tmp_query)){
00127 $this->error('Query successful: '.$tmp_query);
00128 if(DEBUG_SQL_DISPLAY == 1){echo $tmp_query.'<br /><br />';}
00129 return true;
00130 }
00131 $this->error('Query error: '.$tmp_query);
00132 return false;
00133 }
00134
00138 public function fetchArray($type = "MYSQL_ASSOC"){
00139
00140 if(isset($this->result)){
00141 $tmp = @mysql_fetch_array($this->result, constant($type));
00142 }else{
00143 return false;
00144 }
00145 if(DEBUG_SQL_DISPLAY == 1){echo '<pre>'.print_r($tmp, 1).'</pre><br /><hr />';}
00146 return $tmp;
00147 }
00148
00152 public function fetchRow(){
00153
00154 if(isset($this->result)){
00155 $tmp = @mysql_fetch_array($this->result);
00156 }else{
00157 return false;
00158 }
00159 if(DEBUG_SQL_DISPLAY == 1){echo '<pre>'.print_r($tmp, 1).'</pre><br /><hr />';}
00160 return $tmp;
00161 }
00162
00163
00167 public function secure($var){
00168 return mysql_real_escape_string($var);
00169 }
00170
00174 function lastId(){
00175 $this->lastId = mysql_insert_id();
00176
00177 if(DEBUG_SQL_DISPLAY == 1){echo 'LAST ID: '.$this->lastId.'<br />';}
00178 return $this->lastId;
00179 }
00180 }
00181
00182 # Function to secure values if $sql_obj not available
00183 if(!function_exists('mysql_real_escape_string')){
00184 function mysql_real_escape_string($var){
00185 if(get_magic_quotes_gpc()){
00186 return $var;
00187 }else{
00188 return addslashes($var);
00189 }
00190 }
00191 }
00192 ?>