embient.db.php

Go to the documentation of this file.
00001 <?php
00002 
00011 require_once("embient.util.php");
00012 
00017 class EmbientDatabase
00018 {
00020   var $connection;
00021 
00023   var $result;
00024 
00025   // ---------------------------------
00026 
00028   var $_db_host;  
00029 
00031   var $_db_user;
00032 
00034   var $_db_pass;
00035 
00037   var $_db_name;
00038 
00040   var $_prefix;
00041 
00042   // ---------------------------------
00043 
00047   function EmbientDatabase()
00048   {
00049     $this->connection = false;
00050     $this->result     = NULL;
00051 
00052     $this->_db_host    = 'localhost';
00053     $this->_db_user    = 'root';
00054     $this->_db_pass    = '';
00055     $this->_db_name    = '';
00056     $this->_prefix     = '';
00057   }
00058 
00068   function Init($cfg)
00069   {
00070     if ( $this->connection != false )
00071     {
00072       $this->Disconnect();
00073     }
00074 
00075     $this->connection = false;
00076     $this->result     = false;
00077 
00078     $this->_db_host = $cfg['db_host'];
00079     $this->_db_user = $cfg['db_user'];
00080     $this->_db_pass = $cfg['db_passwd'];
00081     $this->_db_name = $cfg['db_name'];
00082     $this->_prefix  = $cfg['prefix'];
00083 
00084     // connect to database!
00085     return $this->Connect();
00086   }
00087 
00094   function Connect()
00095   {
00096     if ( $this->connection != false )
00097     {
00098       return $this->connection;
00099     }
00100 
00101     // Check if MySQL support is present in PHP
00102     if (!function_exists('mysql_connect'))
00103     {
00104       embient_error("mySQL extension for PHP is not installed in this system", __FILE__, __LINE__);
00105       return false;
00106     }
00107 
00108     // connnect to mySQL
00109     $this->connection = mysql_connect($this->_db_host, $this->_db_user, $this->_db_pass);
00110 
00111     if ( ! $this->connection )
00112     {
00113       embient_error(mysql_error(), __FILE__, __LINE__);
00114       return false;
00115     }
00116 
00117     // select the database
00118     if ( ! mysql_select_db($this->_db_name, $this->connection) )
00119     {
00120       embient_error(mysql_error($this->connection), __FILE__, __LINE__);
00121       return false;
00122     }
00123 
00124     return true;
00125   }
00126 
00130   function Disconnect()
00131   {
00132     if ( $this->connection == false )
00133     {
00134       return;
00135     }
00136 
00137     // Check if MySQL support is present in PHP
00138     if (!function_exists('mysql_close'))
00139     {
00140       embient_error("mySQL extension for PHP is not installed in this system", __FILE__, __LINE__);
00141       return;
00142     }
00143 
00144     // disconnnect from mySQL
00145     mysql_close($this->connection);
00146   }
00147 
00161   function Query($query, $file, $line)
00162   {
00163     $this->result = mysql_query($this->_prepare($query), $this->connection);
00164 
00165     if ( ! mysql_errno($this->connection) )
00166     {
00167       return true;
00168     }
00169 
00170     embient_error(mysql_error($this->connection) ." (query = ". $this->_prepare($query) .")", $file, $line);
00171     return false;
00172   }
00173 
00180   function GetRowsCount()
00181   {
00182     if ($this->result)
00183     {
00184       return mysql_num_rows($this->result);
00185     }
00186 
00187     embient_error("Previous query is failed - cannot get rows count", __FILE__, __LINE__);
00188     return false;
00189   }
00190 
00197   function FetchArray()
00198   {
00199     if ($this->result)
00200     {
00201       return mysql_fetch_array($this->result, MYSQL_ASSOC);
00202     }
00203 
00204     embient_error("Previous query is failed, cannot fetch array", __FILE__, __LINE__);
00205     return array();
00206   }
00207 
00214   function FetchAll()
00215   {
00216     $output = array();
00217 
00218     if (!$this->result)
00219     {
00220       embient_error("Previous query is failed, cannot fetch array", __FILE__, __LINE__);
00221     }
00222     else
00223     {
00224       while ($row = mysql_fetch_array($this->result, MYSQL_ASSOC))
00225       {
00226         array_push($output, $row);
00227       }
00228     }
00229 
00230     return $output;
00231   }
00232 
00239   function FetchRow()
00240   {
00241     if ($this->result)
00242     {
00243       return mysql_fetch_row($this->result);
00244     }
00245 
00246     embient_error("Previous query is failed, cannot fetch result", __FILE__, __LINE__);
00247     return NULL;
00248   }
00249 
00265   function GetNextID($tablename, $idname)
00266   {
00267     $query = "LOCK TABLES $tablename WRITE";
00268     $this->Query($query, __FILE__, __LINE__);
00269 
00270     $query = "SELECT id FROM $tablename "
00271            . "WHERE name = '". $idname ."'";
00272     $this->Query($query, __FILE__, __LINE__);
00273 
00274     $id = 0;
00275 
00276     if ( $this->result && $this->GetRowsCount() >= 1)
00277     {
00278       $id = mysql_result($this->result, 0) + 1;
00279     }
00280 
00281     $query = "REPLACE INTO $tablename VALUES ('". $idname ."', '". $id ."')";
00282     $this->Query($query, __FILE__, __LINE__);
00283 
00284     $query = 'UNLOCK TABLES';
00285     $this->Query($query, __FILE__, __LINE__);
00286 
00287     return $id;
00288   }
00289   
00299   function _prepare($sql)
00300   {
00301     return strtr($sql, array('{' => $this->_prefix, '}' => ''));
00302   }
00303 }

Generated on Fri Feb 10 15:05:54 2006 for Embient by  doxygen 1.4.6-NO