database.inc.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. class Database {
  3. private $handle = null;
  4. public function __construct($host, $user, $password, $db) {
  5. $this->handle = new mysqli($host, $user, $password, $db);
  6. }
  7. public function close() {
  8. $this->handle->Close();
  9. }
  10. public function selectQuery($query) {
  11. $stmt = $this->handle->query($query);
  12. if($stmt) {
  13. if($stmt->num_rows == 0) {
  14. $result = NULL;
  15. } else {
  16. $result = array();
  17. while($res = $stmt->fetch_object()) {
  18. $result[] = $res;
  19. }
  20. }
  21. } else {
  22. return null;
  23. }
  24. /*
  25. elseif($stmt->num_rows == 1) {
  26. $result = $stmt->fetch_object();
  27. }
  28. */
  29. $stmt->close();
  30. return $result;
  31. }
  32. public function selectStringQuery($query) {
  33. $stmt = $this->handle->query($query);
  34. if($stmt) {
  35. if($stmt->num_rows == 0) {
  36. $result = NULL;
  37. } else {
  38. $result = $res = $stmt->fetch_array();
  39. }
  40. } else {
  41. return null;
  42. }
  43. /*
  44. elseif($stmt->num_rows == 1) {
  45. $result = $stmt->fetch_object();
  46. }
  47. */
  48. $stmt->close();
  49. return $result[0];
  50. }
  51. public function insertQuery($query) {
  52. $stmt = $this->handle->prepare($query);
  53. $stmt->execute();
  54. $stmt->close();
  55. }
  56. public function updateQuery($query) {
  57. $stmt = $this->handle->prepare($query);
  58. if(!$stmt) {
  59. pa($this->handle); // TODO: better error
  60. } else {
  61. $stmt->execute();
  62. $stmt->close();
  63. }
  64. }
  65. public function removeQuery($query) {
  66. $stmt = $this->handle->prepare($query);
  67. if(!$stmt) {
  68. pa($this->handle);
  69. } else {
  70. $stmt->execute();
  71. $stmt->close();
  72. }
  73. }
  74. }
  75. ?>