Posted By

StrawMan on 06/10/09


Tagged

database php db connect


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vali29
jealousy


Simple DB Connection Class


 / Published in: PHP
 

  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
Posted By: Scooter on June 10, 2009

Those variables should not be public unless you need to get at them from outside the class?

Posted By: StrawMan on August 3, 2009

You're right. I don't recall now why I needed those to be public in the first place. Don't really need them to be public any longer though.

You need to login to post a comment.