Posted By

ChrisBlackwell on 09/13/11


Tagged

mysql databases


Versions (?)

Database Class


 / Published in: PHP
 

  1. class MySQLDatabase {
  2.  
  3. private $connection;
  4.  
  5. function __construct()
  6. {
  7. $this->connect_to_database();
  8. $this->set_charset();
  9. $this->select_database();
  10. }
  11.  
  12. private function connect_to_database()
  13. {
  14. $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS);
  15. if (!$this->connection)
  16. {
  17. die("Database Connection Failure: " . mysql_error($this->connection));
  18. }
  19. }
  20.  
  21. private function set_charset()
  22. {
  23. if (!mysql_set_charset(DB_CHAR, $this->connection))
  24. {
  25. die("Database Character Set Failure: " . mysql_error($this->connection));
  26. }
  27. }
  28.  
  29. private function select_database()
  30. {
  31. if (!mysql_select_db(DB_NAME, $this->connection))
  32. {
  33. die("Database Select Failure: " . mysql_error($this->connection));
  34. }
  35. }
  36.  
  37. public function query($sql_query)
  38. {
  39. $result = mysql_query($sql_query, $this->connection);
  40. if(!$result)
  41. {
  42. die("Error with SQL query: " . mysql_error($this->connection));
  43. }
  44. return $result;
  45. }
  46.  
  47. public function sql_fetch_array($sql_array)
  48. {
  49. return mysql_fetch_array($sql_array);
  50. }
  51.  
  52. public function clean_sql($item_to_sanitize)
  53. {
  54. return mysql_real_escape_string($item_to_sanitize, $this->connection);
  55. }
  56.  
  57. }
  58.  
  59. $db = new MySQLDatabase();

Report this snippet  

You need to login to post a comment.