Advertisement
Guest User

prepare

a guest
Aug 15th, 2017
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3. ini_set('display_errors', TRUE);
  4. ini_set('display_startup_errors', TRUE);
  5.  
  6.  
  7. class DB
  8. {
  9.     // объявление свойства
  10.     protected $host = 'localhost';
  11.     protected $db =   'dfgdfgdfgdfg';  // имя базы
  12.     protected $charset = 'utf8';
  13.     protected $user = 'dfgdfgdfg';       //  имя юзера
  14.     protected $pass = 'dfgdfgdfg';  //  пароль юзера
  15.  
  16.     public function __construct() {
  17.         $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
  18.         $opt = array(
  19.             PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  20.             PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  21.         );
  22.         $this->DBconnect = new PDO($dsn, $this->user, $this->pass, $opt);
  23.     }
  24.  
  25.     // проверка на наличие e-mail в базе 0 - нет, иначе да
  26.     public function checkDoubleMail($table, $column, $email) {
  27.         // Проверим есть ли такой юзер
  28.         $auth = $this->DBconnect->prepare('SELECT COUNT(*) FROM `'.$table.'` WHERE `'.$column.'`=:mail');
  29.         $auth->bindParam(':'.$column, $email, PDO::PARAM_STR);
  30.         $auth->execute();
  31.         $Count = $auth->fetchAll();
  32.         return intval(implode('', $Count[0]));
  33.     }
  34. }
  35.  
  36.  
  37. class Validator
  38. {
  39.     private $DBconnect = null;
  40.  
  41.     public function __construct($db) {
  42.         $this->DBconnect = $db;
  43.     }
  44.     public function checkUnique($table, $column, $email)
  45.     {
  46.         $auth = $this->DBconnect->prepare('SELECT COUNT(*) FROM `'.$table.'` WHERE `'.$column.'`=:mail');
  47.         $auth->bindParam(':'.$column, $email, PDO::PARAM_STR);
  48.         $auth->execute();
  49.         $Count = $auth->fetchAll();
  50.         return intval(implode('', $Count[0]));
  51.     }
  52. }
  53.  
  54. $obj = new DB();
  55. echo '<pre>'; var_dump($obj); echo '</pre>';
  56.  
  57. $val = new Validator($obj);
  58. $check = $val->checkUnique('users', 'mail', 'mail@yandex.ru');
  59. echo '<pre>'; var_dump($check); echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement