Advertisement
Guest User

Untitled

a guest
Apr 7th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. */
  6. class Database
  7. {
  8.  
  9.     private $dbh;    
  10.     private $error;  
  11.     private $stmt;
  12.     private $db_name = DB_NAME;
  13.     private $db_user = DB_USER;
  14.     private $db_pass = DB_PASS;
  15.     private $db_host = DB_HOST;
  16.    
  17.     function __construct()
  18.     {
  19.         $dsn = 'mysql:host=' . $this->db_host . ';dbname=' . $this->db_name;
  20.  
  21.         $dsn_options = array(
  22.             PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  23.             PDO::ATTR_PERSISTENT => true,  
  24.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION  
  25.         );
  26.        
  27.         try {  
  28.             $dbh = new PDO($dsn, $this->db_user, $this->db_pass, $dsn_options);
  29.         } catch (PDOException $e) {  
  30.             $this->error = $e->getMessage();  
  31.         }  
  32.  
  33.     }
  34.  
  35.     public function query ($query)
  36.     {
  37.         $this->stmt = $this->dbh->prepare($query);
  38.     }
  39.  
  40.     public function bind($param, $value, $type = null)
  41.     {
  42.         if (is_null($type)) {  
  43.             switch (true) {  
  44.             case is_int($value):  
  45.                 $type = PDO::PARAM_INT;  
  46.                 break;  
  47.             case is_bool($value):  
  48.                 $type = PDO::PARAM_BOOL;  
  49.                 break;  
  50.             case is_null($value):  
  51.                 $type = PDO::PARAM_NULL;  
  52.                 break;  
  53.             default:  
  54.                 $type = PDO::PARAM_STR;  
  55.             }  
  56.         }
  57.  
  58.         $this->stmt->bindValue($param, $value, $type);  
  59.     }
  60.  
  61.     public function execute()
  62.     {  
  63.         return $this->stmt->execute();  
  64.     }  
  65.  
  66.     public function resultset(){  
  67.         $this->execute();  
  68.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);  
  69.     }
  70.  
  71.     public function single(){  
  72.         $this->execute();  
  73.         return $this->stmt->fetch(PDO::FETCH_ASSOC);  
  74.     }  
  75.  
  76.     public function rowCount(){  
  77.         return $this->stmt->rowCount();  
  78.     }    
  79.  
  80.     public function lastInsertId(){  
  81.         return $this->dbh->lastInsertId();  
  82.     }  
  83.  
  84.     public function beginTransaction(){  
  85.         return $this->dbh->beginTransaction();  
  86.     }  
  87.  
  88.     public function endTransaction(){  
  89.         return $this->dbh->commit();  
  90.     }
  91.  
  92.     public function cancelTransaction(){  
  93.         return $this->dbh->rollBack();  
  94.     }
  95.  
  96.     public function debugDumpParams(){  
  97.         return $this->stmt->debugDumpParams();  
  98.     }
  99.              
  100. }
  101.  
  102.  
  103.  
  104.         $db = new Database;
  105.  
  106.         $db->query('SELECT * FROM servers');
  107.  
  108.         $rows = $database->resultset();  
  109.  
  110.         echo "<pre>";  
  111.         print_r($rows);  
  112.         echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement