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. $this->handle->query("SET NAMES utf8;");
  7. }
  8. public function close() {
  9. $this->handle->Close();
  10. }
  11. public function selectQuery($query) {
  12. $stmt = $this->handle->query($query);
  13. if($stmt) {
  14. if($stmt->num_rows == 0) {
  15. $result = NULL;
  16. } else {
  17. $result = array();
  18. while($res = $stmt->fetch_object()) {
  19. $result[] = $res;
  20. }
  21. }
  22. } else {
  23. return null;
  24. }
  25. /*
  26. elseif($stmt->num_rows == 1) {
  27. $result = $stmt->fetch_object();
  28. }
  29. */
  30. $stmt->close();
  31. return $result;
  32. }
  33. public function selectStringQuery($query) {
  34. $stmt = $this->handle->query($query);
  35. if($stmt) {
  36. if($stmt->num_rows == 0) {
  37. $result = NULL;
  38. } else {
  39. $result = $res = $stmt->fetch_array();
  40. }
  41. } else {
  42. return null;
  43. }
  44. /*
  45. elseif($stmt->num_rows == 1) {
  46. $result = $stmt->fetch_object();
  47. }
  48. */
  49. $stmt->close();
  50. return $result[0];
  51. }
  52. public function insertQuery($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. ?>