database.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. class Database
  3. {
  4. function __construct()
  5. {
  6. $this->customer_table = "customer";
  7. $this->category_table = "category";
  8. $this->product_table = "product";
  9. $this->price_table = "price";
  10. $this->priceset_table = "priceset";
  11. $this->tax_table = "tax";
  12. $this->bill_table = "bill";
  13. $this->bill_product_table = "bill_product";
  14. $this->tour_table = "tour";
  15. $this->assistant_table = "assistant";
  16. $this->group_table = "`group`";
  17. $this->customer_template_table = "customer_template";
  18. $this->ready = false;
  19. }
  20. function connectToDb()
  21. {
  22. $host = getenv('CLOUDRON_MYSQL_HOST');
  23. $user = getenv('CLOUDRON_MYSQL_USERNAME');
  24. $password = getenv('CLOUDRON_MYSQL_PASSWORD');
  25. $db = getenv('CLOUDRON_MYSQL_DATABASE');
  26. $port = getenv('CLOUDRON_MYSQL_PORT');
  27. // $host = "mysql5.nebulon.de";
  28. // $user = "db30675_9";
  29. // $password = "manda1337";
  30. // $db = "db30675_9";
  31. // $host = "localhost";
  32. // $user = "root";
  33. // $password = "";
  34. // $db = "faursprung_net";
  35. $this->db_link = mysqli_connect($host, $user, $password);
  36. if(!$this->db_link)
  37. exit("Error: ".mysqli_connect_error());
  38. if(!mysqli_select_db($this->db_link, $db))
  39. exit("Error: selecting db");
  40. mysqli_set_charset($this->db_link, "latin1");
  41. $this->ready = TRUE;
  42. }
  43. function query($query)
  44. {
  45. if(!$this->ready)
  46. $this->connectToDb();
  47. if(!($result = mysqli_query($this->db_link, $query)))
  48. echo "Error: ".mysqli_error($this->db_link);
  49. return $result;
  50. }
  51. function fetchRows($result)
  52. {
  53. if(!$this->ready)
  54. $this->connectToDb();
  55. $all_rows = array();
  56. while($row = mysqli_fetch_row($result))
  57. $all_rows[] = $row;
  58. return $all_rows;
  59. }
  60. function writeToFile($path, $data)
  61. {
  62. $file_access_ok = true;
  63. $file_open = false;
  64. clearstatcache();
  65. if (file_exists($path))
  66. if (!is_writable($path))
  67. $file_access_ok = false;
  68. if ($file_access_ok and (!$file_handle = fopen($path, "w+b")))
  69. $file_access_ok = false;
  70. if ($file_access_ok)
  71. {
  72. if (!fwrite($file_handle, $data) and ($data != ''))
  73. $file_access_ok = false;
  74. else
  75. $file_open = true;
  76. }
  77. if ($file_open)
  78. fclose($file_handle);
  79. return $file_access_ok;
  80. }
  81. }
  82. ?>