Guest User

database.class.php

a guest
Sep 17th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.61 KB | None | 0 0
  1. <?php
  2. if (!class_exists('Database')){
  3. /**
  4. * <b>Database Connection</b> class.
  5. * @author Php Object Generator // Loksly
  6. * @version 3.0d / PHP5
  7. * @see http://www.phpobjectgenerator.com/
  8. * @copyright Free for personal & commercial use. (Offered under the BSD license)
  9. * @notes modified version
  10.  
  11. you may also want to change your configuration.php file so that
  12. you can configure the encoding character set for your connection,
  13. you only need to add something like this:
  14.  
  15.  
  16. $GLOBALS['configuration']['encoding'] = 'utf8';
  17.  
  18.  
  19. */
  20.  Class Database
  21. {
  22.     public $connection;
  23.     public static $executed_queries = array();
  24.     static $database = NULL;
  25.  
  26.     private function Database()
  27.     {
  28.         $databaseName = $GLOBALS['configuration']['db'];
  29.         $serverName = $GLOBALS['configuration']['host'];
  30.         $databaseUser = $GLOBALS['configuration']['user'];
  31.         $databasePassword = $GLOBALS['configuration']['pass'];
  32.         $databasePort = $GLOBALS['configuration']['port'];     
  33.         $this->connection = mysql_connect ($serverName.":".$databasePort, $databaseUser, $databasePassword);
  34.         if ($this->connection)
  35.         {
  36.             if (!mysql_select_db ($databaseName))
  37.             {
  38.                 throw new Exception('I cannot find the specified database "'.$databaseName.'". Please edit configuration.php.');
  39.             }
  40.             if (isset($GLOBALS['configuration']['encoding']) && !mysql_set_charset($GLOBALS['configuration']['encoding'],$this->connection))
  41.             {
  42.                 throw new Exception('I cannot change the charset at the connection with database "'.$databaseName.'". Please edit configuration.php.');
  43.             }
  44.         }
  45.         else
  46.         {
  47.             throw new Exception('I cannot connect to the database. Please edit configuration.php with your database configuration. database '.$databaseName.' ; host '.$serverName.'');
  48.         }
  49.     }
  50.  
  51.  
  52.     public static function Reset() {
  53.         if (isset(self::$database) && self::$database != NULL)
  54.             mysql_close(self::$database->connection);
  55.         self::$database = NULL;
  56.     }
  57.  
  58.     public static function Connect()
  59.     {
  60.         if (!isset(self::$database) || is_null(self::$database))
  61.         {
  62.             self::$database = new Database();
  63.         }
  64.         return self::$database->connection;
  65.     }
  66.  
  67.     public static function Reader($query, $connection = null)
  68.     {
  69.         is_null($connection) && $connection=self::Connect();
  70.         self::$executed_queries[] = $query;
  71.         $cursor = mysql_query($query, $connection);
  72.         if (!$cursor)
  73.         {
  74.             $traza = debug_backtrace();
  75.             $trazatext = "";
  76.             foreach($traza as $puntotraza)
  77.             {
  78.                 $trazatext .="<br />".print_r($puntotraza,true);
  79.             }
  80.  
  81.             error_log("Error SQL:" . $query.
  82.                         "<br />Error: ".mysql_error(self::$database->connection).
  83.                         "<br />Pila: ".$trazatext );
  84.         }
  85.         return $cursor;
  86.     }
  87.  
  88.     public static function Read($cursor)
  89.     {
  90.         return mysql_fetch_assoc($cursor);
  91.     }
  92.  
  93.     public static function NonQuery($query, $connection = null)
  94.     {
  95.         is_null($connection) && $connection=self::Connect();
  96.         self::$executed_queries[] = $query;
  97.         mysql_query($query, $connection);
  98.         $result = mysql_affected_rows($connection);
  99.         if ($result == -1)
  100.         {
  101.             return false;
  102.         }
  103.         return $result;
  104.     }
  105.  
  106.     public static function Query($query, $connection = null)
  107.     {
  108.         is_null($connection) && $connection=self::Connect();
  109.         self::$executed_queries[] = $query;
  110.         $result = mysql_query($query, $connection);
  111.         return mysql_num_rows($result);
  112.     }
  113.  
  114.     public static function InsertOrUpdate($query, $connection = null)
  115.     {
  116.         is_null($connection) && $connection=self::Connect();
  117.         self::$executed_queries[] = $query;
  118.         $result = mysql_query($query, $connection);
  119.         return intval(mysql_insert_id($connection));
  120.     }
  121.    
  122.     public static function getNumQueries()
  123.     {
  124.         return count(self::$executed_queries);
  125.     }
  126. }
  127. }
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment