database.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $this->db_link = mysqli_connect($host, $user, $password);
  28. if(!$this->db_link)
  29. exit("Error: ".mysqli_connect_error());
  30. if(!mysqli_select_db($this->db_link, $db))
  31. exit("Error: selecting db");
  32. mysqli_set_charset($this->db_link, "latin1");
  33. $this->ready = TRUE;
  34. }
  35. function query($query)
  36. {
  37. if(!$this->ready)
  38. $this->connectToDb();
  39. if(!($result = mysqli_query($this->db_link, $query)))
  40. echo "Error: ".mysqli_error($this->db_link);
  41. return $result;
  42. }
  43. function fetchRows($result)
  44. {
  45. if(!$this->ready)
  46. $this->connectToDb();
  47. $all_rows = array();
  48. while($row = mysqli_fetch_row($result))
  49. $all_rows[] = $row;
  50. return $all_rows;
  51. }
  52. function writeToFile($path, $data)
  53. {
  54. $file_access_ok = true;
  55. $file_open = false;
  56. clearstatcache();
  57. if (file_exists($path))
  58. if (!is_writable($path))
  59. $file_access_ok = false;
  60. if ($file_access_ok and (!$file_handle = fopen($path, "w+b")))
  61. $file_access_ok = false;
  62. if ($file_access_ok)
  63. {
  64. if (!fwrite($file_handle, $data) and ($data != ''))
  65. $file_access_ok = false;
  66. else
  67. $file_open = true;
  68. }
  69. if ($file_open)
  70. fclose($file_handle);
  71. return $file_access_ok;
  72. }
  73. }
  74. ?>