Advertisement
Guest User

Shock

a guest
Aug 21st, 2009
1,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. <?php
  2. class Db {
  3.     static private $connected = false;
  4.     static private $queries = 0;
  5.     static private $config = array (
  6.         'host' => 'localhost',
  7.         'user' => 'Shock',
  8.         'pass' => '',
  9.         'name' => 'FullHierarchy'
  10.     );
  11.  
  12.     public static function start () {
  13.         if(!self::$connected) {
  14.             mysql_connect (self::$config['host'], self::$config['user'], self::$config['pass']);
  15.             mysql_select_db (self::$config['name']);
  16.             mysql_set_charset ('utf8');
  17.             self::$connected = true;
  18.         }
  19.     }
  20.  
  21.     public static function escape ($string) {
  22.         self::start();
  23.         return mysql_real_escape_string($string);
  24.     }
  25.  
  26.     public static function advSelect ($query) {
  27.         return self::select($query, true);
  28.     }
  29.  
  30.     public static function select ($query, $advanced = false) {
  31.         self::start();
  32.         if(!$returned = mysql_unbuffered_query($query)) {
  33.             trigger_error ("Invalid query: " . mysql_error() . "; \nQuery: \"" . $query . "\"", E_USER_ERROR);
  34.         }
  35.         $result = array();
  36.         if ($advanced) {
  37.             $fieldsCount = 0;
  38.             $field = array();
  39.             while ( $row = mysql_fetch_field($returned) ) {
  40.                 $field[] = $row;
  41.                 $fieldsCount++;
  42.             }
  43.             while ( $row = mysql_fetch_row($returned) ) {
  44.                 $advancedRow = array();
  45.                 for($i=0; $i<$fieldsCount; $i++) {
  46.                     $table = $field[$i]->table ? $field[$i]->table : 0;
  47.                     $advancedRow[$table][$field[$i]->name] = $row[$i];
  48.                 }
  49.                 $result[] = $advancedRow;
  50.             }
  51.         } else {
  52.             while ( $row = mysql_fetch_assoc($returned) ) {
  53.                 $result[] = $row;
  54.             }
  55.         }
  56.         mysql_free_result($returned);
  57.         self::$queries++;
  58.         return $result;
  59.     }
  60.  
  61.     public static function execute ($query) {
  62.         self::start();
  63.         $query = trim($query);
  64.         mysql_unbuffered_query ($query);
  65.         self::$queries++;
  66.         return (preg_match('/^insert/i', $query)) ?
  67.             mysql_insert_id() : mysql_affected_rows();
  68.     }
  69.  
  70.     public static function getQueries () {
  71.         return self::$queries;
  72.     }
  73. }
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement