Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2. class Db {
  3.  
  4.     public static $db;
  5.  
  6.     public static function connect()
  7.     {
  8.  
  9.         /*NASTAVENÍ ÚDAJŮ K DATABÁZI */
  10.  
  11.         if($_SERVER['SERVER_ADDR'] == "::1")
  12.         {
  13.             $host = "localhost";
  14.             $database = "android_spy";
  15.             $username = "root";
  16.             $password = "";
  17.         }
  18.  
  19.         else
  20.         {
  21.             $host = "localhost";
  22.             $database = "abcdeshopsk1";
  23.             $username = "db30662";
  24.             $password = "***";
  25.         }
  26.  
  27.  
  28.         $db = new \PDO("mysql:host=$host;dbname=$database", $username, $password);
  29.         self::$db = $db;
  30.  
  31.         $db->query('SET NAMES UTF8');
  32.     }
  33.  
  34.     public static function getMessageById($id)
  35.     {
  36.         $socket = self::$db->prepare("SELECT `body` from `sms` WHERE `id` = ?");
  37.         $socket->execute(array($id));
  38.         $result = $socket->fetch();
  39.         return $result[0];
  40.     }
  41.  
  42.     public static function loginUser($nickname, $password)
  43.     {
  44.         $socket = self::$db->prepare("SELECT user_id, admin FROM users_data WHERE nickname=? AND password=?");
  45.         $socket->execute(array($nickname, $password));
  46.         $result = $socket->fetch();
  47.         return $result[0];
  48.     }
  49.  
  50.     public static function checkDuplicity($nickname)
  51.     {
  52.         $socket = self::$db->prepare("SELECT COUNT(*) FROM users_data WHERE nickname=? LIMIT 1");
  53.         $socket->execute(array($nickname));
  54.         $result = $socket->fetch();
  55.         return $result[0];
  56.     }
  57.  
  58.     public static function registerUser($name, $surname, $nickname, $email, $password)
  59.     {
  60.         $socket = self::$db->prepare("INSERT INTO users_data (name, surname, nickname, email, password) VALUES (?, ?, ?, ?, ?)");
  61.         $socket->execute(array($name, $surname, $nickname, $email, $password));
  62.     }
  63.  
  64.      public static function getLastId()
  65.     {
  66.         return self::$db->lastInsertId();
  67.     }
  68.  
  69.     public static function getAllBattery()
  70.     {
  71.         $socket = self::$db->query('SELECT * FROM `battery` ORDER by `timestamp` DESC ');
  72.         $field = $socket->fetchAll();
  73.         return $field;
  74.     }
  75.  
  76.     public static function getAllTypesOfComunication()
  77.     {
  78.         $socket = self::$db->query('SELECT type FROM im_type_of_communication ORDER by `type` ASC');
  79.         $list = $socket->fetchAll();
  80.  
  81.         $field = array();
  82.         foreach($list as $l)
  83.         {
  84.             $toAdd = array();
  85.             $toAdd["type"] = $l["type"];
  86.  
  87.             $toAdd["count"] = self::getCountOfComunication($l["type"]);
  88.  
  89.             array_push($field, $toAdd);
  90.             unset($toAdd);
  91.  
  92.         }
  93.         return $field;
  94.     }
  95.  
  96.     private static function getCountOfComunication($type)
  97.     {
  98.         $socket = self::$db->query("SELECT COUNT(*) FROM im WHERE `type` = '$type'");
  99.         $result = $socket->fetch();
  100.         return $result[0];
  101.     }
  102.  
  103.     public static function getAllSms()
  104.     {
  105.         $socket = self::$db->query('SELECT * FROM `sms` ORDER by `timestamp` DESC ');
  106.         $field = $socket->fetchAll();
  107.         return $field;
  108.     }
  109.  
  110.     public static function getAllMessagesByType($type)
  111.     {
  112.         $socket = self::$db->query("SELECT * FROM `im` WHERE `type` = '" . $type ."' ORDER by `timestamp` DESC");
  113.         $field = $socket->fetchAll();
  114.         return $field;
  115.     }
  116.  
  117.     public static function getMessageByIdAndType($type, $id)
  118.     {
  119.         $socket = self::$db->prepare("SELECT `message` from `im` WHERE `type` = ? AND `id` = ?");
  120.         $socket->execute(array($type, $id));
  121.         $result = $socket->fetch();
  122.         return $result[0];
  123.     }
  124.  
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement