Guest User

Untitled

a guest
Jul 21st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5. * DB Class
  6. * Trida pro praci s databazi
  7. * @package classes
  8. */
  9.  
  10.  
  11. require_once("Config.php");
  12.  
  13.  
  14. class MySQL {
  15. public $hadler=null;
  16.  
  17. static private $instance;
  18.  
  19. /**
  20. *
  21. * @return object $instance
  22. */
  23. static public function getInstance() {
  24. if(self::$instance==NULL){
  25. self::$instance = new MySQL();
  26. }
  27. return self::$instance;
  28. }
  29.  
  30. /**
  31. * Konstruktor pro pripojeni k databazi.
  32. */
  33. private function __construct() {
  34.  
  35. $host = Config::getInstance()->getHost();
  36. $user = Config::getInstance()->getUser();
  37. $pass = Config::getInstance()->getPass();
  38. $database = Config::getInstance()->getDb();
  39.  
  40. // pripojeni k db serveru
  41. $this->hadler = new mysqli($host,$user,$pass);
  42.  
  43.  
  44. if(mysqli_connect_errno()) {
  45. throw new Exception("Nepodařilo se připojit k serveru.");
  46. }
  47.  
  48. // vyber databaze
  49. if(!$this->hadler->select_db($database)) {
  50. throw new Exception("Nepodařilo se vybrat databázi.");
  51. }
  52.  
  53. // nastaveni kodovani
  54. $sql = 'SET CHARACTER SET UTF8';
  55. $q = $this->query($sql);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. /**
  62. * Funkce uzavre databazove spojeni.
  63. */
  64. function close() {
  65. $this->hadler->close();
  66. }
  67.  
  68. /**
  69. * Funkce pro volani db dotazu
  70. * @param string $query
  71. * @return string $result
  72. *
  73. */
  74. function query($query) {
  75. $result = $this->hadler->query($query);
  76.  
  77. if(!$result) {
  78. throw new Exception("SQL příkaz selhal: ". $query);
  79. return false;
  80. }
  81. else {
  82.  
  83. return $result;
  84. }
  85. }
  86.  
  87.  
  88. /**
  89. * Funkce vrati associativni pole hodnot
  90. * @param string $result odkaz na db dotaz
  91. * @return array $rows
  92. */
  93. function fetch_assoc($result) {
  94. $pole = array();
  95.  
  96. while ($row = mysqli_fetch_assoc($result)) {
  97. echo $row["userid"];
  98. echo $row["fullname"];
  99. echo $row["userstatus"];
  100. $pole[] = $row ;
  101. }
  102.  
  103. return $pole;
  104.  
  105. }
  106.  
  107.  
  108. function fetch_array($result) {
  109. $array = mysqli_fetch_array($result);
  110. return $array;
  111. }
  112.  
  113.  
  114. /**
  115. * Funkce vrati pocet ovlivnenych radku z predchoziho query dotazu
  116. * @param string $result odkaz na db dotaz
  117. * @return int $count pocet ovlivnenych radku
  118. */
  119. function affected_rows($result) {
  120. return mysqli_affected_rows($result);
  121. }
  122.  
  123.  
  124. /**
  125. * Funkce vrati pole s jednim zaznamem na zaklade query dotazu
  126. * @param $result odkaz na db dotaz
  127. * @return array $array vicerozmerne pole s ciselnymi klici
  128. */
  129. function fetch_row($result) {
  130. return $array = mysqli_fetch_row($result);
  131. }
  132.  
  133.  
  134. /**
  135. * Vraci pocet radku na zaklade query
  136. * @param $result odkaz na db dotaz
  137. * @return int $count pocet radku z vysledku db dotazu
  138. */
  139. function num_rows($result) {
  140. return mysqli_num_rows($result);
  141. }
  142.  
  143.  
  144. /**
  145. * Funkce vrati pocet sloupcu provedeneho query dotazu
  146. * @param $result odkaz na db dotaz
  147. * @return int $count pocet vracenych sloupcu
  148. */
  149. function num_fields($result) {
  150. return mysqli_num_fields($result);
  151. }
  152.  
  153.  
  154. /**
  155. * Vrati id posledne vlozeneho zaznamu
  156. */
  157. function lastId() {
  158. return mysqli_insert_id($this->hadler);
  159. }
  160.  
  161.  
  162. /**
  163. * Funkce upravi retezec query proti SQL Injection
  164. * @param string $unescaped_string potencialne nebezpecny retezec
  165. * @return string $string bezpecny retezec
  166. */
  167. function escape_string($unescaped_string) {
  168. $string = parent::escape_string($unescaped_string);
  169. return $string;
  170. }
  171.  
  172.  
  173.  
  174. }
  175.  
  176. $db = MySQL::getInstance();
  177.  
  178. ?>
Add Comment
Please, Sign In to add comment