Guest

Gamebak Class DB

By: a guest on Aug 9th, 2011  |  syntax: PHP  |  size: 1.30 KB  |  hits: 100  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2. /*
  3. db_class.php
  4. Created by C. Alexandru for codebox.ro and coderi.ro
  5.  
  6. Usage:
  7. $db->query("SQL");
  8. $db->raw_query("SQL");
  9. $db->secure_string($_GET['something']);
  10. $db->secure_string($_POST['something']);
  11.  
  12. $db->close_connection();
  13.  
  14. Don't forget to close MySql connection
  15. DB Setup
  16. */
  17. defined('DB_SERVER')?NULL:define("DB_SERVER", "localhost");
  18. defined('DB_USER')?NULL  :define("DB_USER", "root");
  19. defined('DB_PASS')?NULL  :define("DB_PASS", "");
  20. defined('DB_NAME')?NULL  :define("DB_NAME", "v2ge");
  21.  
  22.  
  23.  
  24.  
  25. class MysqlDatabase{
  26.         public $connection;
  27.         function __construct(){$this->open_connection();}
  28.                
  29.         public function open_connection(){$this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die("DB connection error");mysql_select_db(DB_NAME,$this->connection);}
  30.        
  31.         public function close_connection() {
  32.                 if(isset($this->connection)) {
  33.                         mysql_close($this->connection);
  34.                         unset ($this->connection);
  35.                 }
  36.         }
  37.         public function query($sql){
  38.                 return mysql_query($sql,$this->connection);
  39.         }
  40.         public function raw_query($sql){
  41.             return mysql_unbuffered_query($sql,$this->connection);
  42.         }
  43.         public function secure_string($string){return htmlspecialchars(htmlentities(mysql_real_escape_string($string),ENT_QUOTES),ENT_QUOTES);}
  44. }
  45.  
  46.  
  47. $db = new MysqlDatabase();
  48.  
  49.  
  50. ?>