Guest User

Untitled

a guest
Jul 25th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. <?php
  2.  
  3. class DB {
  4.  
  5. private $host;
  6. private $user;
  7. private $pass;
  8. private $database;
  9. private $connection;
  10. public $result;
  11.  
  12. public function __construct() {
  13. $this->load_config();
  14. try {
  15. $this->connect();
  16. }
  17. catch(Exception $e) {
  18. echo $e->getMessage;
  19. exit;
  20. }
  21. }
  22.  
  23. private function load_config() {
  24. $config = parse_ini_file("config.ini");
  25. $this->host = $config['host'];
  26. $this->user = $config['user'];
  27. $this->pass = $config['pass'];
  28. $this->database = $config['database'];
  29. }
  30.  
  31. private function connect() {
  32. $this->connection = @mysql_connect($this->host, $this->user, $this->pass);
  33. if(!$this->connection) {
  34. throw new Exception("<b>ERROR:</b> Failed to establish MySQL connection!");
  35. }
  36. $db = @mysql_select_db($this->database, $this->connection);
  37. if(!$db) {
  38. throw new Exception("<b>ERROR:</b> Failed to select database!");
  39. }
  40. }
  41.  
  42. public function query($query) {
  43. if(is_resource($this->result)) {
  44. mysql_free_result($this->result);
  45. }
  46. $this->result = @mysql_query($query, $this->connection);
  47. if(!$this->result) {
  48. throw new Exception("<b>ERROR:</b> Failed to execute query!");
  49. }
  50. }
  51.  
  52. private function disconnect() {
  53. if(is_resource($this->result)) {
  54. mysql_free_result($this->result);
  55. }
  56. if(is_resource($this->connection)) {
  57. mysql_close($this->connection);
  58. }
  59. }
  60.  
  61. public function __destruct() {
  62. $this->disconnect();
  63. }
  64. }
  65. ?>
Add Comment
Please, Sign In to add comment