db/db_mysqli_class.php

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     // Define SQL values (server, user, pwd, database...)
00017     function __construct($tmpserver='', $tmpuser='', $tmppwd='', $tmpdb='');
00018     // Set error msg
00019     # function error($msg=false);
00020     // Connection Database
00021     function connect();
00022     // Select Database
00023     function selectdb();
00024     // Execute query
00025     function execute($tmp_query);
00026     // Fetch Array
00027     function fetchArray($type = "MYSQLI_ASSOC");
00028     // Fetch row
00029     function fetchRow();
00030     // Secure variable
00031     function secure($var);
00032     // Get last ID
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         $this->lastId = 0;
00066         $this->error = false;
00067         $this->errno = false;
00068         $this->server = $tmpserver;
00069         $this->user = $tmpuser;
00070         $this->pwd = $tmppwd;
00071         $this->database = $tmpdb;
00072         if(!function_exists('mysqli_connect')){
00073             if(function_exists('mysqli_connect_error')) {
00074                 $this->error = mysqli_connect_error();
00075             }
00076             if(function_exists('mysqli_connect_errno')) {
00077                 $this->errorno = mysqli_connect_errno();
00078             }
00079             trigger_error('Function mysqli_connect() does not exists. mysqli extension is not enabled?', E_USER_ERROR);
00080             return false;
00081         }
00082         return true;
00083     }
00084 
00088     public function getConnect() 
00089     {
00090         return $this->connect;
00091     }
00092     
00093     
00097     private function error($msg=false){
00098         $this->error = $msg;
00099     }
00100 
00104     public function connect(){
00105         if($this->connect = @mysqli_connect(
00106             $this->server,
00107             $this->user,
00108             $this->pwd
00109             )){
00110             $this->error('Connection using mysqli_connect SUCCESSFUL');
00111             return true;
00112         }
00113         $this->error('Connection using mysqli_connect FAILED');
00114         return false;
00115     }
00116 
00117     public function selectdb(){
00118         if(@mysqli_select_db($this->connect, $this->database)){
00119             $this->error('Base '.$this->database.' exists');
00120             return true;
00121         }
00122         $this->error('Base '.$this->database.' does NOT exist');
00123         return false;
00124     }
00125 
00126 
00130     public function execute($tmp_query){
00131 
00132         if($this->result = @mysqli_query($this->connect, $tmp_query)){
00133             $this->error('Query successful: '.$tmp_query);
00134             if(DEBUG_SQL_DISPLAY == 1){echo $tmp_query.'<br /><br />';}
00135             return true;
00136         }
00137         $this->error('Query error: '.$tmp_query);
00138         return false;
00139     }
00140 
00144     public function fetchArray($type = "MYSQLI_ASSOC"){
00145 
00146         if(isset($this->result)){
00147             $tmp = @mysqli_fetch_array($this->result, constant($type));
00148         }else{
00149             return false;
00150         }
00151         if(DEBUG_SQL_DISPLAY == 1){echo '<pre>'.print_r($tmp, 1).'</pre><br /><hr />';}
00152         return $tmp;
00153     }
00154 
00158     public function fetchRow(){
00159 
00160         if(isset($this->result)){
00161             $tmp = @mysqli_fetch_array($this->result);
00162         }else{
00163             return false;
00164         }
00165         if(DEBUG_SQL_DISPLAY == 1){echo '<pre>'.print_r($tmp, 1).'</pre><br /><hr />';}
00166         return $tmp;
00167     }
00168 
00169 
00173     public function secure($var){
00174         return mysqli_real_escape_string($this->connect, $var);
00175     }
00176 
00180     function lastId(){
00181         $this->lastId = mysqli_insert_id($this->connect);
00182 
00183         if(DEBUG_SQL_DISPLAY == 1){echo 'LAST ID: '.$this->lastId.'<br />';}
00184         return $this->lastId;
00185     }
00186 }
00187 
00188 # Function to secure values if $sql_obj not available
00189 if(!function_exists('mysql_real_escape_string')){
00190     function mysql_real_escape_string($var){
00191         if(get_magic_quotes_gpc()){
00192             return $var;
00193         }else{
00194             return addslashes($var);
00195         }
00196     }
00197 }
00198 ?>

Generated on Sun Mar 4 17:19:28 2007 for streber by  doxygen 1.5.1-p1