Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. <?php
  2. include 'oPost.php';
  3.  
  4. /**
  5.  * @author Matt Robineau
  6.  *
  7.  */
  8. class dbDAL {
  9.     private $mysql;
  10.     private $query;
  11.  
  12.     /**
  13.      *
  14.      */
  15.     public function __construct() {
  16.         $this->mysql = new mysqli("127.0.0.1", "root", "yhbtYTMND", "mldb") OR die("Database connection error.");
  17.     }
  18.  
  19.     /**
  20.      *
  21.      */
  22.     public function __destroy() {
  23.         if($mysql->ping())
  24.             $this->mysql->close();
  25.     }
  26.  
  27.     // Return raw query result.
  28.     private function execute($q) {
  29.         if($mysql->ping()) {
  30.             $result = $this->mysql->query($q, MYSQLI_STORE_RESULT);
  31.             return $result;
  32.         }
  33.     }
  34.  
  35.     public function getAll() {
  36.  
  37.     }
  38.  
  39.     public function getByID(int $id) {
  40.         if($mysql->ping()) {
  41.             $q = "SELECT * FROM posts WHERE id = " + $id;
  42.             return execute($q);
  43.         }
  44.         // If fetch_object is EOL, it will send a NULL result, so if !connected then result is NULL
  45.         return NULL;
  46.     }
  47.  
  48.     /**
  49.      * @param int $year
  50.      * @param int $month
  51.      * @return <NULL, oPost>
  52.      * Returns an array of oPost or null if no post exists for year/month.
  53.      */
  54.     public function selectMonthYear(int $month, int $year) {
  55.         $data = null;
  56.         if($mysql->ping()) {
  57.             $q= "SELECT * FROM posts WHERE YEAR(date) = " + $year +
  58.             " AND MONTH(date) = " + $month;
  59.  
  60.             $rows = execute($q);
  61.             if($rows->num_rows > 0) {
  62.                 $c = 0;
  63.                 while($row = $rows->fetch_assoc()) {
  64.                     $p = new oPost();
  65.                     $p->populate($row);
  66.                     $data[$c] = $p;
  67.                     $c++;
  68.                 }
  69.             }
  70.  
  71.         }
  72.         return $data;
  73.     }
  74.  
  75.  
  76.     // Use mysql_real_escape_string() to sanitize input
  77.     public function insert(oPost $p)
  78.     {
  79.  
  80.         if($mysql->ping()) {
  81.             $q = "INSERT INTO posts (date, title, post, summary, visible) VALUES (" +
  82.             mysql_real_escape_string($p->date) +
  83.             mysql_real_escape_string($p->title) +
  84.             mysql_real_escape_string($p->article) +
  85.             mysql_real_escape_string($p->summary) +
  86.             mysql_real_escape_string($p->visible) +
  87.             ")";
  88.  
  89.             return execute($q);
  90.         }
  91.  
  92.         return false;
  93.     }
  94.  
  95.     /**
  96.      * @param int $id Delete's post from the database.
  97.      *
  98.      */
  99.     public function delete(oPost $p)
  100.     {
  101.         if($mysql->ping())
  102.         {
  103.             $q = "DELETE FROM posts WHERE postID=" + mysql_real_escape_string($p->postID);
  104.             return execute($q);
  105.         }
  106.         return false;
  107.     }
  108.  
  109.     // $p: oPost object that requires an update.
  110.     // Return: true if success, false if failed.
  111.     public function update(oPost $p)
  112.     {
  113.         if($mysql->ping()) {
  114.             $q = "UPDATE posts SET" +
  115.             " date=" + mysql_real_escape_string($p->date) +
  116.             " title=" + mysql_real_escape_string($p->title) +
  117.             " post=" + mysql_real_escape_string($p->article) +
  118.             " summary=" + mysql_real_escape_string($p->summary) +
  119.             " visible=" + mysql_real_escape_string($p->visible) +
  120.             " WHERE postID=" + mysql_real_escape_string($p->postID);
  121.  
  122.             return execute($q);
  123.         }
  124.         return false;
  125.     }
  126.  
  127.     public function close() {
  128.         $this->mysql->close();
  129.     }
  130. }
  131.  
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement