Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * DB Class
- * Trida pro praci s databazi
- * @package classes
- */
- require_once("Config.php");
- class MySQL {
- public $hadler=null;
- static private $instance;
- /**
- *
- * @return object $instance
- */
- static public function getInstance() {
- if(self::$instance==NULL){
- self::$instance = new MySQL();
- }
- return self::$instance;
- }
- /**
- * Konstruktor pro pripojeni k databazi.
- */
- private function __construct() {
- $host = Config::getInstance()->getHost();
- $user = Config::getInstance()->getUser();
- $pass = Config::getInstance()->getPass();
- $database = Config::getInstance()->getDb();
- // pripojeni k db serveru
- $this->hadler = new mysqli($host,$user,$pass);
- if(mysqli_connect_errno()) {
- throw new Exception("Nepodařilo se připojit k serveru.");
- }
- // vyber databaze
- if(!$this->hadler->select_db($database)) {
- throw new Exception("Nepodařilo se vybrat databázi.");
- }
- // nastaveni kodovani
- $sql = 'SET CHARACTER SET UTF8';
- $q = $this->query($sql);
- }
- /**
- * Funkce uzavre databazove spojeni.
- */
- function close() {
- $this->hadler->close();
- }
- /**
- * Funkce pro volani db dotazu
- * @param string $query
- * @return string $result
- *
- */
- function query($query) {
- $result = $this->hadler->query($query);
- if(!$result) {
- throw new Exception("SQL příkaz selhal: ". $query);
- return false;
- }
- else {
- return $result;
- }
- }
- /**
- * Funkce vrati associativni pole hodnot
- * @param string $result odkaz na db dotaz
- * @return array $rows
- */
- function fetch_assoc($result) {
- $pole = array();
- while ($row = mysqli_fetch_assoc($result)) {
- echo $row["userid"];
- echo $row["fullname"];
- echo $row["userstatus"];
- $pole[] = $row ;
- }
- return $pole;
- }
- function fetch_array($result) {
- $array = mysqli_fetch_array($result);
- return $array;
- }
- /**
- * Funkce vrati pocet ovlivnenych radku z predchoziho query dotazu
- * @param string $result odkaz na db dotaz
- * @return int $count pocet ovlivnenych radku
- */
- function affected_rows($result) {
- return mysqli_affected_rows($result);
- }
- /**
- * Funkce vrati pole s jednim zaznamem na zaklade query dotazu
- * @param $result odkaz na db dotaz
- * @return array $array vicerozmerne pole s ciselnymi klici
- */
- function fetch_row($result) {
- return $array = mysqli_fetch_row($result);
- }
- /**
- * Vraci pocet radku na zaklade query
- * @param $result odkaz na db dotaz
- * @return int $count pocet radku z vysledku db dotazu
- */
- function num_rows($result) {
- return mysqli_num_rows($result);
- }
- /**
- * Funkce vrati pocet sloupcu provedeneho query dotazu
- * @param $result odkaz na db dotaz
- * @return int $count pocet vracenych sloupcu
- */
- function num_fields($result) {
- return mysqli_num_fields($result);
- }
- /**
- * Vrati id posledne vlozeneho zaznamu
- */
- function lastId() {
- return mysqli_insert_id($this->hadler);
- }
- /**
- * Funkce upravi retezec query proti SQL Injection
- * @param string $unescaped_string potencialne nebezpecny retezec
- * @return string $string bezpecny retezec
- */
- function escape_string($unescaped_string) {
- $string = parent::escape_string($unescaped_string);
- return $string;
- }
- }
- $db = MySQL::getInstance();
- ?>
Add Comment
Please, Sign In to add comment