database.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class Database
  3. {
  4. function Database()
  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->bill_import_table = "bill_import";
  18. $this->bill_product_import_table = "bill_product_import";
  19. $this->ready = False;
  20. }
  21. function connectToDb()
  22. {
  23. $host = getenv('CLOUDRON_MYSQL_HOST');
  24. $user = getenv('CLOUDRON_MYSQL_USERNAME');
  25. $password = getenv('CLOUDRON_MYSQL_PASSWORD');
  26. $db = getenv('CLOUDRON_MYSQL_DATABASE');
  27. $port = getenv('CLOUDRON_MYSQL_PORT');
  28. $this->db_link = mysqli_connect($host, $user, $password, $db, $port);
  29. if(!$this->db_link) {
  30. echo "Error: ".mysqli_connect_error();
  31. exit(1);
  32. }
  33. mysqli_set_charset($this->db_link, "latin1");
  34. $this->ready = True;
  35. }
  36. function query($query)
  37. {
  38. if(!$this->ready)
  39. $this->connectToDb();
  40. if(!($result = mysqli_query($this->db_link, $query)))
  41. echo "Error: ".mysqli_error($this->db_link);
  42. return $result;
  43. }
  44. function fetchRows($result)
  45. {
  46. if(!$this->ready)
  47. $this->connectToDb();
  48. $all_rows = array();
  49. while($row = mysqli_fetch_row($result))
  50. $all_rows[] = $row;
  51. return $all_rows;
  52. }
  53. function writeToFile($path, $data)
  54. {
  55. $file_access_ok = true;
  56. $file_open = false;
  57. clearstatcache();
  58. if (file_exists($path))
  59. if (!is_writable($path))
  60. $file_access_ok = false;
  61. if ($file_access_ok and (!$file_handle = @fopen($path, "w+b")))
  62. $file_access_ok = false;
  63. if ($file_access_ok)
  64. {
  65. if (!fwrite($file_handle, $data) and ($data != ''))
  66. $file_access_ok = false;
  67. else
  68. $file_open = true;
  69. }
  70. if ($file_open)
  71. fclose($file_handle);
  72. }
  73. }
  74. ?>