Guest User

Untitled

a guest
Sep 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Description of mysql
  5. *
  6. * @author Janek Ostendorf
  7. * @package DSE
  8. */
  9.  
  10. class mysql {
  11.  
  12. /**
  13. * Server Host
  14. * @var string
  15. */
  16. var $host;
  17.  
  18. /**
  19. * Benutzername
  20. * @var string
  21. */
  22. var $user;
  23.  
  24. /**
  25. * Passwort
  26. * @var string
  27. */
  28. var $password;
  29.  
  30. /**
  31. * Datenbankname
  32. * @var string
  33. */
  34. var $database;
  35.  
  36. /**
  37. * Verbindungsstatus
  38. * @var bool
  39. */
  40. var $status = 0;
  41.  
  42. /**
  43. * MySQL Verbindungs ID
  44. * @var mixed
  45. */
  46. var $conID;
  47.  
  48. /**
  49. * MySQL Ergebnis
  50. * @var resource
  51. */
  52. var $result;
  53.  
  54. /**
  55. * MySQL Fehlernummer
  56. * @var int
  57. */
  58. var $errornum;
  59.  
  60. /**
  61. * MySQL Fehlermeldung
  62. * @var string
  63. */
  64. var $errormessage;
  65.  
  66. /**
  67. * Anzahl der ausgefuehrten Queries
  68. * @var int
  69. */
  70. var $queries = 0;
  71.  
  72. /**
  73. * Dauer der Queries
  74. * @var int
  75. */
  76. var $query_time = 0;
  77.  
  78.  
  79. /**
  80. * Verbindet zum MySQL Server
  81. * @param string $host MySQL Server
  82. * @param string $user MySQL Benutzer
  83. * @param string $password MySQL Passwort
  84. * @return bool
  85. */
  86. function connect($host, $user, $password) {
  87.  
  88. $this->host = $host;
  89. $this->user = $user;
  90. $this->password = $password;
  91.  
  92. if($this->status == 0) {
  93.  
  94. if(!$this->conID = mysql_connect($this->host, $this->user, $this->password, true)) {
  95. $this->status = 0;
  96. $this->errormessage = mysql_error();
  97. $this->errornum = mysql_errno();
  98. return 0;
  99. }
  100. else {
  101. $this->status = 1;
  102. return 1;
  103. }
  104.  
  105. }
  106.  
  107. }
  108.  
  109. /**
  110. * Waehlt eine Datenbank aus
  111. * @param string $database Datenbankname
  112. * @return bool Erfolg
  113. */
  114. function selectDb($database) {
  115.  
  116. if($this->status == 0) {
  117. return 0;
  118. }
  119. else {
  120. $this->database = $database;
  121. if(!mysql_select_db($this->database, $this->conID)) {
  122. $this->errormessage = mysql_error();
  123. $this->errornum = mysql_errno();
  124. return 0;
  125. }
  126. return 1;
  127. }
  128.  
  129. }
  130.  
  131. /**
  132. * Fuehrt die Abfrage aus
  133. * @param string $query Query
  134. * @return resource MySQL Ergebnis
  135. */
  136. function query($query) {
  137.  
  138. if($this->status == 0) {
  139. return 0;
  140. }
  141. else {
  142.  
  143. $time_start = microtime(true);
  144. $this->result = mysql_query($query, $this->conID);
  145. $this->query_time += microtime(true) - $time_start;
  146.  
  147. $this->queries++;
  148.  
  149. if(!$this->result) {
  150. $this->errormessage = mysql_error();
  151. $this->errornum = mysql_errno();
  152. return 0;
  153. }
  154. else {
  155. return $this->result;
  156. }
  157. }
  158.  
  159. }
  160.  
  161. /**
  162. * Setzt den Zeichensatz der MySQL Verbindung
  163. * @param string $charset Zeichensatz
  164. * @return bool Erfolg
  165. */
  166. function setCharset($charset) {
  167.  
  168. if($this->status == 0) {
  169. return 0;
  170. }
  171. else {
  172. if(!mysql_set_charset($charset, $this->conID)) {
  173. return 0;
  174. }
  175. else {
  176. return 1;
  177. }
  178. }
  179.  
  180. }
  181.  
  182. /**
  183. * Gibt die letzte Fehlermeldung aus
  184. * @return string|bool "Fehlernummer : Fehlermeldung" oder false
  185. */
  186. function getLastError() {
  187.  
  188. if(!empty($this->errormessage) && !empty($this->errornum)) {
  189. return ($this->errornum . " : " . $this->errormessage);
  190. }
  191. else {
  192. return 0;
  193. }
  194.  
  195. }
  196.  
  197. /**
  198. * Gibt die Anzahl der ausgefuehrten Queries aus
  199. * @return int Anzahl der Queries
  200. */
  201. function getQueries() {
  202.  
  203. return $this->queries;
  204.  
  205. }
  206.  
  207. /**
  208. * Gibt die Summe der Dauer aller Queries aus
  209. * @return int Dauer der Queries in Sekunden
  210. */
  211. function getQueryTime() {
  212.  
  213. return $this->query_time;
  214.  
  215. }
  216.  
  217. /**
  218. * Schliesst die Verbindung zum Server
  219. * @return bool Erfolg
  220. */
  221. function disconnect() {
  222.  
  223. if($this->status == 1) {
  224. if(!mysql_close($this->conID)) {
  225. return 0;
  226. }
  227. else {
  228. $this->status = 0;
  229. return 1;
  230. }
  231. }
  232. else {
  233. return 0;
  234. }
  235. }
  236.  
  237. }
  238.  
  239. ?>
Add Comment
Please, Sign In to add comment