Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Db {
- static private $connected = false;
- static private $queries = 0;
- static private $config = array (
- 'host' => 'localhost',
- 'user' => 'Shock',
- 'pass' => '',
- 'name' => 'FullHierarchy'
- );
- public static function start () {
- if(!self::$connected) {
- mysql_connect (self::$config['host'], self::$config['user'], self::$config['pass']);
- mysql_select_db (self::$config['name']);
- mysql_set_charset ('utf8');
- self::$connected = true;
- }
- }
- public static function escape ($string) {
- self::start();
- return mysql_real_escape_string($string);
- }
- public static function advSelect ($query) {
- return self::select($query, true);
- }
- public static function select ($query, $advanced = false) {
- self::start();
- if(!$returned = mysql_unbuffered_query($query)) {
- trigger_error ("Invalid query: " . mysql_error() . "; \nQuery: \"" . $query . "\"", E_USER_ERROR);
- }
- $result = array();
- if ($advanced) {
- $fieldsCount = 0;
- $field = array();
- while ( $row = mysql_fetch_field($returned) ) {
- $field[] = $row;
- $fieldsCount++;
- }
- while ( $row = mysql_fetch_row($returned) ) {
- $advancedRow = array();
- for($i=0; $i<$fieldsCount; $i++) {
- $table = $field[$i]->table ? $field[$i]->table : 0;
- $advancedRow[$table][$field[$i]->name] = $row[$i];
- }
- $result[] = $advancedRow;
- }
- } else {
- while ( $row = mysql_fetch_assoc($returned) ) {
- $result[] = $row;
- }
- }
- mysql_free_result($returned);
- self::$queries++;
- return $result;
- }
- public static function execute ($query) {
- self::start();
- $query = trim($query);
- mysql_unbuffered_query ($query);
- self::$queries++;
- return (preg_match('/^insert/i', $query)) ?
- mysql_insert_id() : mysql_affected_rows();
- }
- public static function getQueries () {
- return self::$queries;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement