Simple DB Connection Class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ////////////////////////////////////
  2. //Simple DB Connection class.
  3. ////////////////////////////////////
  4. class dbconnect {
  5. //Variables.
  6. private $host;
  7. private $user;
  8. private $password;
  9. private $database;
  10. private $lnk;
  11. public $result;
  12. ////////////////////////////
  13. //Constructor.
  14. ////////////////////////////
  15. public function __construct($a, $b, $c, $d) {
  16. $this -> host = $a;
  17. $this -> user = $b;
  18. $this -> password = $c;
  19. $this -> database = $d;
  20. }
  21. ////////////////////////////
  22. //Public class functions.
  23. ////////////////////////////
  24. //Query
  25. public function func_query($qstring) {
  26. $this -> func_connect();
  27. mysql_query("SET NAMES 'utf8'");
  28. $this -> result = mysql_query($qstring);
  29. return $this -> result;
  30. mysql_close($this -> lnk);
  31. mysql_free_result($this -> result);
  32. }
  33. ////////////////////////////
  34. //Private class functions.
  35. ////////////////////////////
  36. //Connect.
  37. private function func_connect() {
  38. $this -> lnk = mysql_connect($this -> host, $this -> user, $this -> password, true) OR die('Could not connect to the database - Why: '.mysql_error());
  39. mysql_select_db($this -> database) OR die('Could not find database: '.mysql_error());
  40. }
  41. ////////////////////////////
  42. //Destructor.
  43. ////////////////////////////
  44. public function __destruct() {
  45. unset ($this -> user, $this -> password);
  46. }
  47. }
  48. ////////////////////////////////////

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.