Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. <?php
  2. class Database implements DataSource
  3. {
  4. private $link;
  5. private $host;
  6. private $user;
  7. private $pass;
  8. private $result;
  9.  
  10. //Foutmelding die in meerdere functies gebruikt wordt
  11. private $noresult = "Er is geen resultset, voer eerst een query uit!";
  12.  
  13. //Constructor met de vereiste parameters
  14. public function Database($host, $user, $pass, $database)
  15. {
  16. if($host && $user)
  17. {
  18. $this->host = $host;
  19. $this->user = $user;
  20. $this->pass = $pass;
  21. $this->Connect();
  22. if(!mysql_select_db($database, $this->link))
  23. throw new Exception("Kan de database " . $database . " niet selecteren");
  24. }
  25. mysql_set_charset("utf8", $this->link);
  26. }
  27.  
  28. //Connectie naar de mysql database leggen
  29. private function Connect()
  30. {
  31. $this->link = mysql_connect($this->host, $this->user, $this->pass, true);
  32.  
  33. if(!$this->link)
  34. {
  35. throw new Exception("Kan geen connectie met de database maken.");
  36. }
  37. }
  38.  
  39. //Query functie uitvoeren en een fout geven als het fout gaat
  40. public function Query($query = "")
  41. {
  42. if($query != "")
  43. {
  44. $this->result = mysql_query($query, $this->link);
  45. }
  46. else
  47. {
  48. throw new Exception("De query is leeg!");
  49. }
  50.  
  51. if(!$this->result)
  52. {
  53. throw new Exception( "Fout tijdens query: ($query)");
  54. }
  55.  
  56. return $this->result;
  57. }
  58.  
  59. public function Fetch_Object($objecttype=null, $result=NULL)
  60. {
  61. if(!$result)
  62. {
  63. $result = $this->result;
  64. }
  65.  
  66. if($result)
  67. {
  68. if(is_null($objecttype))
  69. return mysql_fetch_object($result);
  70. else
  71. return mysql_fetch_object($result, $objecttype);
  72. }
  73. else
  74. {
  75. throw new Exception($this->noresult);
  76. }
  77. }
  78.  
  79. //Fetch_Assoc functie van mysql zodat je $row["naam"] kunt gebruiken
  80. public function Fetch_Assoc($result=NULL)
  81. {
  82. if(!$result)
  83. {
  84. $result = $this->result;
  85. }
  86.  
  87. if($result)
  88. {
  89. return mysql_fetch_assoc($result);
  90. }
  91. else
  92. {
  93. throw new Exception($this->noresult);
  94. }
  95. }
  96.  
  97. //Fetch_Array functie van mysql zodat je $row[0] kunt gebruiken
  98. public function Fetch_Array()
  99. {
  100. if($this->result)
  101. {
  102. return mysql_fetch_array($this->result);
  103. }
  104. else
  105. {
  106. throw new Exception($this->noresult);
  107. }
  108. }
  109.  
  110. //Fetch row functie van mysql voor het ophalen van 1 rij
  111. public function Fetch_Row()
  112. {
  113. if($this->result)
  114. {
  115. return mysql_fetch_row($this->result);
  116. }
  117. else
  118. {
  119. throw new Exception($this->noresult);
  120. }
  121. }
  122.  
  123. public function Mysql_Batch_Query($query)
  124. {
  125. $this->querys[] = "Batchquery ". $query;
  126. $batch_connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
  127. $batch_connection->multi_query($query);
  128. $batch_connection->close();
  129. }
  130.  
  131. //Deze functie geeft het laatst toegevoegde id terug
  132. public function GetLastid()
  133. {
  134. return mysql_insert_id($this->link);
  135. }
  136.  
  137. //Deze functie geeft het aantal geraakte rijen van de laatste query terug
  138. public function Nuw_Rows()
  139. {
  140. if($this->result)
  141. {
  142. return mysql_num_rows($this->result);
  143. }
  144. else
  145. {
  146. throw new Exception($this->noresult);
  147. }
  148. }
  149.  
  150. public function Affected_Rows()
  151. {
  152. return mysql_affected_rows();
  153. }
  154. }
  155. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement