Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

db.inc.php

Go to the documentation of this file.
00001 <?
00002     //our query timer...
00003     $db_exec_time = 0;
00004     $db_query_data = array();
00005     $page_queries = 0;
00006     $sqlQueryCount = 0;
00007 
00008     function dbConnect($server, $username, $password, $new_link=NULL, $client_flags=NULL)
00009     {
00010         return mysql_connect($server, $username, $password, $new_link, $client_flags);
00011     }
00012     
00013     function dbEscape($string)
00014     {
00015         return mysql_real_escape_string($string);
00016     }
00017 
00018     function dbGetTypeValues($type)
00019     {
00020         if (!preg_match("/^(?:set|enum)\(('.*')\)\$/", $type, $matches))
00021             throw new Exception("dbGetTypeValues: invalid type '$type'");
00022 
00023         $values = $matches[1];
00024         if (!preg_match_all("/'((?:[^']|'')*)'/", $values, $matches))
00025             throw new Exception("dbGetTypeValues: invalid type value '$values'");
00026 
00027         $values = $matches[1];
00028         foreach ($values as $key => $value)
00029             $values[$key] = str_replace("''", "'", $value);
00030 
00031         return $values;
00032     }
00033     
00034     function dbExecute($sql, $id = false, $link = null)
00035     {
00036         global $sqlQueryCount;
00037         $sqlQueryCount++;
00038 
00039         if (Config::get('db_track'))
00040         {
00041             global $sqlQueryArray;
00042             $sqlQueryArray[] = $sql;
00043         }
00044         
00045         if (!mysql_query($sql))
00046             dbError();
00047 
00048         if ($id === -1)
00049             return mysql_affected_rows();
00050         if ($id)
00051             return mysql_insert_id();
00052     }
00053 
00054     function dbFetchDesc($result)
00055     {
00056         if ($row = dbFetchAssoc($result))
00057             return array(
00058                 "name" => $row["Field"],
00059                 "type" => $row["Type"],
00060                 "null" => (bool)$row["Null"],
00061                 "default" => $row["Default"],
00062                 "comment" => $row["Comment"],
00063                 "extra" => $row["Extra"],
00064                 "key" => $row["Key"],
00065             );
00066 
00067         return false;
00068     }
00069     
00070     function dbIsResultSet($result)
00071     {
00072             return is_resource($result) && get_resource_type($result) == "mysql result";
00073     }
00074 
00075     function dbHasError()
00076     {
00077         return (mysql_error() != '');
00078     }
00079     
00080     function dbError()
00081     {
00082         throw new DbException(mysql_error());
00083     }
00084 
00085     function dbInsertId()
00086     {
00087         return mysql_insert_id();
00088     }
00089     
00090     function dbNumRows($result)
00091     {
00092         return mysql_num_rows($result);
00093     }
00094 
00095     function dbGetNumRows($rs)
00096     {
00097         return dbNumRows($rs);
00098     }
00099 
00100     function dbSelect($db)
00101     {
00102         return mysql_select_db($db);
00103     }
00104 
00105     function dbQuery( $query, $conn=NULL )
00106     { 
00107         global $link;
00108         
00109         global $sqlQueryCount;
00110         $sqlQueryCount++;
00111 
00112         if (Config::get('db_track'))
00113         {
00114             global $sqlQueryArray;
00115             $sqlQueryArray[] = $query;
00116         }
00117         
00118         //what database??
00119         if($conn == NULL)
00120             $conn = $link;
00121         
00122         //do the actual query...
00123         $result = mysql_query($query, $conn);
00124 
00125         if (dbHasError())
00126             dbError();
00127 
00128         return $result;
00129     }
00130     
00131     function dbTryResult($result)
00132     {
00133         switch (mysql_num_rows($result))
00134         {
00135             case 0:
00136                 return null;
00137 
00138             case 1:
00139                 return mysql_result($result, 0);
00140 
00141             default:
00142                 throw new Exception("dbResult: " . mysql_num_rows($result) . " results found");
00143         }
00144     }
00145 
00146     function dbTryFetchAssoc($result)
00147     {
00148         return mysql_fetch_assoc($result);
00149     }
00150     
00151     function dbFetchAssoc( $result )
00152     {
00153         if (dbHasError())
00154             dbError();
00155 
00156         return mysql_fetch_assoc($result);
00157     }
00158 
00159     function dbFetchArray( $result )
00160     { 
00161         if (dbHasError())
00162             dbError();
00163 
00164         return mysql_fetch_array($result);
00165     }
00166 
00167     if (Config::get('db_host'))
00168     {
00169         global $link;
00170     
00171         $count = 0;
00172         while (!$link && $count <= 5)
00173         {
00174             if (!$link = @dbConnect(Config::get('db_host'), Config::get('db_user'), Config::get('db_pass')));
00175             {
00176                 sleep($count);
00177                 $count++;
00178             }
00179         }
00180 
00181         if ($link)
00182         {
00183             $connected_to_db = 1;
00184             
00185             //switch to our database...    
00186             $db = dbSelect(Config::get('db_name'));
00187         }
00188         //let them know!
00189         else
00190             throw new DbError("I'm sorry, I couldn't connect to the database.  Please try again later.");
00191     }
00192 ?>

Generated on Fri Oct 27 12:26:39 2006 for BaseJumper by doxygen 1.3.9.1