SHARE
TWEET

Untitled

a guest Jan 26th, 2019 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Database {
  2.  
  3.     // defining some variables
  4.     private $host;
  5.     private $user;
  6.     private $pass;
  7.     private $bd;
  8.  
  9.     protected $dbh;
  10.  
  11.  
  12.     /*
  13.  
  14.     method construct
  15.     This method will be used everytime our class is called
  16.  
  17.     */
  18.  
  19.     public function __construct() {
  20.         $this->host = HOST;
  21.         $this->user = USER;
  22.         $this->pass = PASS;
  23.         $this->bd = DB;
  24.  
  25.         // database connection
  26.         $dsn = 'mysql:host=' . HOST . ';dbname=' . DB;
  27.         // Set options
  28.         $options = array(
  29.             PDO::ATTR_PERSISTENT        => true,
  30.             PDO::ATTR_ERRMODE           => PDO::ERRMODE_EXCEPTION,
  31.             PDO::ATTR_EMULATE_PREPARES  => false,
  32.             PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  33.             PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  34.         );
  35.         //Create a new PDO instance
  36.         try {
  37.             $this->dbh = new PDO($dsn, USER, PASS, $options);
  38.  
  39.             return $this->dbh;
  40.         }
  41.         // Catch any errors
  42.         catch(PDOException $e) {
  43.             die($e->getMessage());
  44.         }
  45.     }
  46. }
  47.    
  48. class Posts extends Database {
  49.  
  50.     /* FRONTEND METHODS */
  51.     // method to select all posts
  52.     // uses a query predefined and an array of parameters
  53.     // returns the array of results
  54.     public function selectPosts($query, $params = array()) {
  55.         // prepare the query to bind params
  56.         $stmt = $this->dbh->prepare($query);
  57.  
  58.         // binds the params
  59.         foreach($params as $key => $val) {
  60.             if(is_string($val)) {
  61.                 $stmt->bindParam($key, $val, PDO::PARAM_STR);
  62.             }
  63.             elseif(is_numeric($val)) {
  64.                 $stmt->bindParam($key, $val, PDO::PARAM_INT);
  65.             }
  66.         }
  67.         // execute query after parameters are binded
  68.         $stmt->execute();
  69.  
  70.         // returns the result
  71.         return $stmt->fetchAll();
  72.     }
  73.  
  74.  
  75.     // method to select a single posts
  76.     // uses a query predefined and an array of parameters
  77.     // returns a single row
  78.     public function selectSingle($query, $params = array()) {
  79.         // prepare the query to bind params
  80.         $stmt = $this->dbh->prepare($query);
  81.  
  82.         // binds the params
  83.         foreach($params as $key => $val) {
  84.             if(is_string($val)) {
  85.                 $stmt->bindParam($key, $val, PDO::PARAM_STR);
  86.             }
  87.             elseif(is_numeric($val)) {
  88.                 $stmt->bindParam($key, $val, PDO::PARAM_INT);
  89.             }
  90.         }
  91.  
  92.         // execute query after parameters are binded
  93.         $stmt->execute();
  94.  
  95.         // returns the result - a single row
  96.         return $stmt->fetch();
  97.     }
  98.  
  99.  
  100.  
  101.     /* BACKEND METHODS */
  102.  
  103.     // insert posts into database
  104.     // passes just the parameters
  105.     public function insertPost($params = array()) {
  106.         $uery = "INSERT INTO posts (...) VALUES (...)";
  107.  
  108.         // prepare the query to bind params
  109.         $stmt = $this->dbh->prepare($query);
  110.  
  111.         // binds the params
  112.         foreach($params as $key => $val) {
  113.             if(is_string($val)) {
  114.                 $stmt->bindParam($key, $val, PDO::PARAM_STR);
  115.             }
  116.             elseif(is_numeric($val)) {
  117.                 $stmt->bindParam($key, $val, PDO::PARAM_INT);
  118.             }
  119.         }
  120.  
  121.         // execute query after parameters are binded
  122.         return $stmt->exeute() ? true : false;
  123.     }
  124.  
  125.     // delete posts from the database
  126.     // needs to get the id of the post to delete
  127.     public function deletePost($id) {
  128.         $query = "DELETE FROM posts WHERE id = :id";
  129.  
  130.         // prepare the query to bind params
  131.         $stmt = $this->dbh->prepare($query);
  132.  
  133.         // bind the params
  134.         $stmt->bindParam(':id', $id, PDO::PARAM_INT);
  135.  
  136.         // executes query after params are binded
  137.         return $stmt->exeute() ? true : false;
  138.  
  139.     }
  140.  
  141.     // updates a post
  142.     // passes an array of parameters to bind
  143.     public function updatePost($params = array()) {
  144.         $query = "UPDATE posts SET ... WHERE id = :id";
  145.  
  146.         // prepare the query to bind params
  147.         $stmt = $this->dbh->prepare($query);
  148.  
  149.         // binds the params
  150.         foreach($params as $key => $val) {
  151.             if(is_string($val)) {
  152.                 $stmt->bindParam($key, $val, PDO::PARAM_STR);
  153.             }
  154.             elseif(is_numeric($val)) {
  155.                 $stmt->bindParam($key, $val, PDO::PARAM_INT);
  156.             }
  157.         }
  158.  
  159.         // execute query after parameters are binded
  160.         return $stmt->exeute() ? true : false;
  161.     }
  162. }
  163.    
  164. $posts = new Posts();
  165.  
  166. $query = "SELECT * FROM posts WHERE posts.id > :n";
  167. $params = [':n' => 6];
  168. foreach($posts->selectPosts($query, $params) as $post) {
  169.     echo $post['id']. ', ';
  170. }
  171.    
  172. $posts = new Posts();
  173. foreach($posts->selectByIdBiggerThan(6) as $post) {
  174.     echo $post['id']. ', ';
  175. }
  176.    
  177. public function selectByIdBiggerThan($id) {
  178.     $query = "SELECT * FROM posts WHERE id > ?";
  179.     return $this->db->selectAll($query, [$id]));
  180. }
  181.    
  182. public function run($sql, $args = [])
  183. {
  184.     if (!$args)
  185.     {
  186.          return $this->query($sql);
  187.     }
  188.     $stmt = $this->pdo->prepare($sql);
  189.     $stmt->execute($args);
  190.     return $stmt;
  191. }
  192. public function selectAll($sql, $params = []) {
  193.     return $this->run($sql, $params)->fetchAll();
  194. }
  195. public function selectSingle($sql, $params = []) {
  196.     return $this->run($sql, $params)->fetch();
  197. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top