database.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, $db, $port);
  28. if(!$this->db_link)
  29. exit("Error: ".mysqli_connect_error());
  30. mysqli_set_charset($this->db_link, "latin1");
  31. $this->ready = True;
  32. }
  33. function query($query)
  34. {
  35. if(!$this->ready)
  36. $this->connectToDb();
  37. if(!($result = mysqli_query($this->db_link, $query)))
  38. echo "Error: ".mysqli_error($this->db_link);
  39. return $result;
  40. }
  41. function fetchRows($result)
  42. {
  43. if(!$this->ready)
  44. $this->connectToDb();
  45. $all_rows = array();
  46. while($row = mysqli_fetch_row($result))
  47. $all_rows[] = $row;
  48. return $all_rows;
  49. }
  50. }
  51. ?>