Database.class.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Config {
  3. require_once 'Settings.class.php';
  4. class Database {
  5. // database credentials
  6. private $host;
  7. private $db_name = "iimt_mathcloud";
  8. private $username = "newuser";
  9. private $password = "password";
  10. //
  11. protected $cnx = null;
  12. /**
  13. *
  14. */
  15. public function __construct($name) {
  16. $this->db_name = $name;
  17. $this->host = Settings::getDatabaseHostname();
  18. }
  19. /**
  20. * Get the database connection.
  21. */
  22. public function getConnection() {
  23. if ($this->cnx == null) {
  24. try {
  25. $this->cnx = new \PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password, array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
  26. }
  27. catch(PDOException $e){
  28. throw new Exception($e->getMessage());
  29. }
  30. }
  31. return $this->cnx;
  32. }
  33. /**
  34. * Get the database connection (admin).
  35. */
  36. public function getAdminConnection() {
  37. if ($this->cnx == null) {
  38. try {
  39. $this->cnx = new \PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name."_admin", $this->username, $this->password, array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
  40. }
  41. catch(PDOException $e){
  42. throw new Exception($e->getMessage());
  43. }
  44. }
  45. return $this->cnx;
  46. }
  47. /**
  48. * Get the database connection (audit).
  49. */
  50. public function getAuditConnection() {
  51. if ($this->cnx == null) {
  52. try {
  53. $this->cnx = new \PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name."_audit", $this->username, $this->password, array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
  54. }
  55. catch(PDOException $e){
  56. throw new Exception($e->getMessage());
  57. }
  58. }
  59. return $this->cnx;
  60. }
  61. }
  62. }
  63. ?>