database.inc.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. echo $query;
  53. $stmt = $this->handle->prepare($query);
  54. $stmt->execute();
  55. $stmt->close();
  56. }
  57. public function updateQuery($query) {
  58. $stmt = $this->handle->prepare($query);
  59. if(!$stmt) {
  60. pa($this->handle);
  61. } else {
  62. $stmt->execute();
  63. $stmt->close();
  64. }
  65. }
  66. public function removeQuery($query) {
  67. $stmt = $this->handle->prepare($query);
  68. if(!$stmt) {
  69. pa($this->handle);
  70. } else {
  71. $stmt->execute();
  72. $stmt->close();
  73. }
  74. }
  75. }
  76. ?>